supabase-cli

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Supabase CLI Skill

Supabase CLI 命令参考大全

Complete CLI command reference for Supabase development.
Supabase开发的完整CLI命令参考。

Quick Reference

快速参考

TaskCommand
Install CLI
npm install supabase --save-dev
Login
supabase login
Init project
supabase init
Link to remote
supabase link --project-ref <ref>
Start local stack
supabase start
Stop local stack
supabase stop
Check status
supabase status
Create migration
supabase migration new <name>
Push to remote
supabase db push
Pull from remote
supabase db pull
Reset local DB
supabase db reset
Generate types
supabase gen types typescript --local > types.ts
任务命令
安装CLI
npm install supabase --save-dev
登录
supabase login
初始化项目
supabase init
关联远程项目
supabase link --project-ref <ref>
启动本地服务栈
supabase start
停止本地服务栈
supabase stop
查看状态
supabase status
创建迁移文件
supabase migration new <name>
推送至远程
supabase db push
从远程拉取
supabase db pull
重置本地数据库
supabase db reset
生成类型定义
supabase gen types typescript --local > types.ts

Installation

安装

bash
undefined
bash
undefined

NPM (recommended for projects)

NPM(推荐用于项目)

npm install supabase --save-dev npx supabase [command]
npm install supabase --save-dev npx supabase [command]

Homebrew (macOS/Linux)

Homebrew(适用于macOS/Linux)

brew install supabase/tap/supabase
brew install supabase/tap/supabase

Scoop (Windows)

Scoop(适用于Windows)

scoop bucket add supabase https://github.com/supabase/scoop-bucket.git scoop install supabase

**Prerequisites**: Docker (required for local development)
scoop bucket add supabase https://github.com/supabase/scoop-bucket.git scoop install supabase

**前置要求**:Docker(本地开发必需)

Project Setup

项目设置

Initialize Project

初始化项目

bash
supabase init
Creates:
  • supabase/config.toml
    - Configuration file
  • supabase/migrations/
    - Migration files
  • supabase/seed.sql
    - Seed data
bash
supabase init
创建以下文件/目录:
  • supabase/config.toml
    - 配置文件
  • supabase/migrations/
    - 迁移文件目录
  • supabase/seed.sql
    - 初始数据文件

Link to Remote Project

关联远程项目

bash
supabase link --project-ref <project-ref>
Get project ref from: Dashboard → Project Settings → General
bash
supabase link --project-ref <project-ref>
项目参考ID获取路径:控制台 → 项目设置 → 常规设置

Local Development

本地开发

Start Local Stack

启动本地服务栈

bash
supabase start
Starts:
  • PostgreSQL database
  • PostgREST API
  • GoTrue Auth
  • Realtime server
  • Storage service
  • Studio dashboard
  • Inbucket (email testing)
Output URLs:
API URL: http://localhost:54321
DB URL: postgresql://postgres:postgres@localhost:54322/postgres
Studio URL: http://localhost:54323
Inbucket URL: http://localhost:54324
bash
supabase start
启动以下服务:
  • PostgreSQL数据库
  • PostgREST API
  • GoTrue 认证服务
  • Realtime 服务器
  • 存储服务
  • Studio 控制台
  • Inbucket(邮件测试工具)
输出URL:
API URL: http://localhost:54321
DB URL: postgresql://postgres:postgres@localhost:54322/postgres
Studio URL: http://localhost:54323
Inbucket URL: http://localhost:54324

Stop Local Stack

停止本地服务栈

bash
undefined
bash
undefined

Stop and save data

停止并保存数据

supabase stop
supabase stop

Stop and delete all data

停止并删除所有数据

supabase stop --no-backup
undefined
supabase stop --no-backup
undefined

Check Status

查看状态

bash
supabase status
Shows URLs, API keys, and service status.
bash
supabase status
显示服务URL、API密钥和服务状态。

Database Commands

数据库命令

Create Migration

创建迁移文件

bash
supabase migration new create_users_table
Creates:
supabase/migrations/<timestamp>_create_users_table.sql
bash
supabase migration new create_users_table
创建文件:
supabase/migrations/<timestamp>_create_users_table.sql

Generate Migration from Changes

根据变更生成迁移文件

bash
undefined
bash
undefined

Make changes locally, then generate migration

在本地修改后,生成迁移文件

supabase db diff -f my_schema_changes
supabase db diff -f my_schema_changes

Diff against specific schemas

对比指定模式

supabase db diff -f changes --schema public,extensions
undefined
supabase db diff -f changes --schema public,extensions
undefined

Apply Migrations

应用迁移

bash
undefined
bash
undefined

Apply pending migrations locally

在本地应用待处理的迁移

supabase migration up
supabase migration up

Push to remote

推送至远程

supabase db push
supabase db push

Preview what would be pushed

预览将要推送的内容

supabase db push --dry-run
undefined
supabase db push --dry-run
undefined

Pull Remote Changes

拉取远程变更

bash
supabase db pull
Creates migration from Dashboard changes.
bash
supabase db pull
根据控制台的变更创建迁移文件。

Reset Local Database

重置本地数据库

bash
supabase db reset
Recreates database and applies all migrations.
bash
supabase db reset
重新创建数据库并应用所有迁移文件。

List Migrations

列出迁移文件

bash
supabase migration list
Shows local vs remote migration status.
bash
supabase migration list
显示本地与远程的迁移状态对比。

Rollback Migrations

回滚迁移

bash
undefined
bash
undefined

Rollback last n migrations

回滚最近n个迁移

supabase migration down --count 1
supabase migration down --count 1

Rollback specific number

回滚指定数量的迁移

supabase migration down --count 3
undefined
supabase migration down --count 3
undefined

Squash Migrations

合并迁移文件

bash
undefined
bash
undefined

Combine multiple migrations into one

将多个迁移文件合并为一个

supabase migration squash --version 20240101000000
supabase migration squash --version 20240101000000

Squash all migrations up to version

合并指定版本之前的所有迁移文件

supabase migration squash --version <target_version>

Useful for cleaning up migration history before release.
supabase migration squash --version <target_version>

适用于发布前清理迁移历史。

Type Generation

类型生成

Generate TypeScript Types

生成TypeScript类型定义

bash
undefined
bash
undefined

From local database

从本地数据库生成

supabase gen types typescript --local > database.types.ts
supabase gen types typescript --local > database.types.ts

From linked remote

从关联的远程项目生成

supabase gen types typescript --linked > database.types.ts
supabase gen types typescript --linked > database.types.ts

From specific project

从指定项目生成

supabase gen types typescript --project-id "your-id" > database.types.ts
undefined
supabase gen types typescript --project-id "your-id" > database.types.ts
undefined

Edge Functions

Edge Functions

Create Function

创建函数

bash
supabase functions new hello-world
Creates:
supabase/functions/hello-world/index.ts
bash
supabase functions new hello-world
创建文件:
supabase/functions/hello-world/index.ts

Serve Locally

本地运行

bash
undefined
bash
undefined

Serve all functions

运行所有函数

supabase functions serve
supabase functions serve

Serve specific function

运行指定函数

supabase functions serve hello-world
supabase functions serve hello-world

With env file and no JWT

使用环境变量文件且不验证JWT

supabase functions serve --env-file .env --no-verify-jwt
undefined
supabase functions serve --env-file .env --no-verify-jwt
undefined

Deploy

部署

bash
undefined
bash
undefined

Deploy all functions

部署所有函数

supabase functions deploy
supabase functions deploy

Deploy specific function

部署指定函数

supabase functions deploy hello-world
supabase functions deploy hello-world

Deploy without JWT verification (for webhooks)

部署时关闭JWT验证(适用于Webhook)

supabase functions deploy webhook-handler --no-verify-jwt
undefined
supabase functions deploy webhook-handler --no-verify-jwt
undefined

Test Function

测试函数

bash
curl -i --request POST \
  'http://localhost:54321/functions/v1/hello-world' \
  --header 'Authorization: Bearer <ANON_KEY>' \
  --header 'Content-Type: application/json' \
  --data '{"name":"Functions"}'
bash
curl -i --request POST \
  'http://localhost:54321/functions/v1/hello-world' \
  --header 'Authorization: Bearer <ANON_KEY>' \
  --header 'Content-Type: application/json' \
  --data '{"name":"Functions"}'

Secrets Management

密钥管理

bash
undefined
bash
undefined

Set secret

设置密钥

supabase secrets set API_KEY=abc123
supabase secrets set API_KEY=abc123

Set multiple secrets

设置多个密钥

supabase secrets set API_KEY=abc123 DB_PASSWORD=secret
supabase secrets set API_KEY=abc123 DB_PASSWORD=secret

Set from .env file

从.env文件导入密钥

supabase secrets set --env-file .env
supabase secrets set --env-file .env

List secrets

列出密钥

supabase secrets list
supabase secrets list

Remove secret

删除密钥

supabase secrets unset API_KEY
undefined
supabase secrets unset API_KEY
undefined

Project Management

项目管理

bash
undefined
bash
undefined

List organizations

列出组织

supabase orgs list
supabase orgs list

List projects

列出项目

supabase projects list
supabase projects list

Create project

创建项目

supabase projects create <name> --org-id <id> --region <region>
supabase projects create <name> --org-id <id> --region <region>

Delete project

删除项目

supabase projects delete --project-ref <ref>
supabase projects delete --project-ref <ref>

Update remote config from local

从本地更新远程配置

supabase projects update-config
undefined
supabase projects update-config
undefined

Database Inspection

数据库检查

bash
undefined
bash
undefined

Cache hit ratio

缓存命中率

supabase inspect db cache-hit --linked
supabase inspect db cache-hit --linked

Unused indexes

未使用的索引

supabase inspect db unused-indexes --local
supabase inspect db unused-indexes --local

Table bloat

表膨胀情况

supabase inspect db bloat --linked
supabase inspect db bloat --linked

Long running queries

长时间运行的查询

supabase inspect db long-running-queries --linked
supabase inspect db long-running-queries --linked

Active connections

活跃连接数

supabase inspect db role-connections --linked
undefined
supabase inspect db role-connections --linked
undefined

Backup & Restore

备份与恢复

Backup

备份

bash
undefined
bash
undefined

Dump roles

导出角色

supabase db dump --linked -f roles.sql --role-only
supabase db dump --linked -f roles.sql --role-only

Dump schema

导出模式

supabase db dump --linked -f schema.sql
supabase db dump --linked -f schema.sql

Dump data

导出数据

supabase db dump --linked -f data.sql --use-copy --data-only
undefined
supabase db dump --linked -f data.sql --use-copy --data-only
undefined

Restore

恢复

bash
psql --single-transaction \
  --file roles.sql \
  --file schema.sql \
  --command 'SET session_replication_role = replica' \
  --file data.sql \
  --dbname [CONNECTION_STRING]
bash
psql --single-transaction \
  --file roles.sql \
  --file schema.sql \
  --command 'SET session_replication_role = replica' \
  --file data.sql \
  --dbname [CONNECTION_STRING]

Common Flags

通用参数

FlagDescription
--debug
Verbose output
--local
Use local database
--linked
Use linked remote
--project-ref <ref>
Specific project
--db-url <url>
Connection string
--dry-run
Preview without executing
-f <file>
Output to file
参数描述
--debug
详细输出
--local
使用本地数据库
--linked
使用关联的远程项目
--project-ref <ref>
指定项目参考ID
--db-url <url>
数据库连接字符串
--dry-run
预览操作不实际执行
-f <file>
输出至指定文件

References

参考资料

  • local-development.md - Local stack setup
  • migrations.md - Migration workflow
  • config-toml.md - Configuration options
  • local-development.md - 本地服务栈设置文档
  • migrations.md - 迁移工作流文档
  • config-toml.md - 配置选项文档