Supabase CLI工具 #

一、CLI安装 #

1.1 安装方式 #

bash
# macOS - Homebrew
brew install supabase/tap/supabase

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

# Windows - Chocolatey
choco install supabase

# Linux
curl -fsSL https://sup.supabase.com/install.sh | bash

# npm
npm install -g supabase

1.2 验证安装 #

bash
supabase --version

二、认证命令 #

2.1 登录 #

bash
# 登录Supabase账号
supabase login

# 查看登录状态
supabase projects list

2.2 链接项目 #

bash
# 链接到远程项目
supabase link --project-ref your-project-ref

# 查看链接状态
supabase link

三、项目命令 #

3.1 初始化项目 #

bash
# 初始化本地项目
supabase init

# 生成的文件
# supabase/
# ├── config.toml
# ├── migrations/
# ├── functions/
# └── seed.sql

3.2 启动本地服务 #

bash
# 启动所有本地服务
supabase start

# 输出
# API URL: http://localhost:54321
# DB URL: postgresql://postgres:postgres@localhost:54322/postgres
# Studio URL: http://localhost:54323
# Inbucket URL: http://localhost:54324
# anon key: ...
# service_role key: ...

3.3 停止服务 #

bash
# 停止服务
supabase stop

# 停止并删除数据
supabase stop --no-backup

3.4 查看状态 #

bash
# 查看服务状态
supabase status

四、数据库命令 #

4.1 迁移管理 #

bash
# 创建新迁移
supabase migration new create_users_table

# 应用迁移到本地
supabase db push

# 应用迁移到远程
supabase db push --linked

# 查看迁移列表
supabase migration list

# 重置本地数据库
supabase db reset

4.2 数据库操作 #

bash
# 执行SQL
supabase db execute --sql "SELECT * FROM users"

# 执行SQL文件
supabase db execute -f queries/select_users.sql

# 连接数据库
supabase db psql

# 远程数据库连接
supabase db psql --linked

4.3 数据库差异 #

bash
# 比较本地和远程差异
supabase db diff

# 生成迁移文件
supabase db diff --schema public --use-migra

五、函数命令 #

5.1 函数管理 #

bash
# 创建新函数
supabase functions new my-function

# 本地运行函数
supabase functions serve

# 部署函数
supabase functions deploy my-function

# 部署所有函数
supabase functions deploy

# 删除函数
supabase functions delete my-function

5.2 函数日志 #

bash
# 查看函数日志
supabase functions logs my-function

# 实时日志
supabase functions logs my-function --follow

5.3 密钥管理 #

bash
# 设置密钥
supabase secrets set MY_SECRET=value

# 从文件设置
supabase secrets set --env-file .env

# 查看密钥列表
supabase secrets list

# 删除密钥
supabase secrets delete MY_SECRET

六、类型生成 #

6.1 生成TypeScript类型 #

bash
# 从本地数据库生成
supabase gen types typescript --local > lib/database.types.ts

# 从远程项目生成
supabase gen types typescript --project-id your-project-id > lib/database.types.ts

# 使用链接项目
supabase gen types typescript --linked > lib/database.types.ts

七、项目列表 #

7.1 列出项目 #

bash
# 列出所有项目
supabase projects list

# 创建新项目
supabase projects create my-project

# 删除项目
supabase projects delete my-project

八、配置文件 #

8.1 config.toml #

toml
# 项目配置
[project]
name = "my-project"

# API配置
[api]
port = 54321
schemas = ["public", "storage", "graphql_public"]
extra_search_path = ["public", "extensions"]
max_rows = 1000

# 数据库配置
[db]
port = 54322
shadow_port = 54320
major_version = 15

# 认证配置
[auth]
site_url = "http://localhost:3000"
additional_redirect_urls = ["http://localhost:3000/auth/callback"]
jwt_expiry = 3600
enable_signup = true

# 存储配置
[storage]
file_size_limit = "50MiB"

# Edge Functions配置
[edge_runtime]
enabled = true

九、常用命令速查 #

9.1 项目管理 #

命令 说明
supabase init 初始化项目
supabase start 启动本地服务
supabase stop 停止服务
supabase status 查看状态
supabase link 链接远程项目

9.2 数据库 #

命令 说明
supabase migration new 创建迁移
supabase db push 应用迁移
supabase db reset 重置数据库
supabase db diff 查看差异

9.3 函数 #

命令 说明
supabase functions new 创建函数
supabase functions serve 本地运行
supabase functions deploy 部署函数
supabase functions logs 查看日志

十、总结 #

CLI要点:

功能 命令
登录 supabase login
初始化 supabase init
启动 supabase start
迁移 supabase migration new
函数 supabase functions

下一步,让我们学习迁移管理!

最后更新:2026-03-28