Supabase安装与配置 #
一、环境准备 #
1.1 系统要求 #
| 系统 | 要求 |
|---|---|
| macOS | 10.15+ |
| Windows | Windows 10+ |
| Linux | Ubuntu 18.04+ / Debian 10+ |
1.2 必备工具 #
text
必备工具
├── Node.js 18+
├── npm / yarn / pnpm
├── Git
└── Docker (本地开发需要)
1.3 检查环境 #
bash
# 检查Node.js版本
node --version
# 应该显示 v18.x.x 或更高
# 检查npm版本
npm --version
# 检查Git
git --version
# 检查Docker (可选)
docker --version
二、Supabase CLI安装 #
2.1 macOS安装 #
bash
# 使用Homebrew安装
brew install supabase/tap/supabase
# 验证安装
supabase --version
2.2 Windows安装 #
powershell
# 使用Scoop安装
scoop bucket add supabase https://github.com/supabase/scoop-bucket.git
scoop install supabase
# 或使用Chocolatey
choco install supabase
# 验证安装
supabase --version
2.3 Linux安装 #
bash
# 使用安装脚本
curl -fsSL https://sup.supabase.com/install.sh | bash
# 或使用npm
npm install -g supabase
# 验证安装
supabase --version
2.4 npm全局安装 #
bash
# 适用于所有平台
npm install -g supabase
# 验证安装
supabase --version
三、CLI常用命令 #
3.1 命令概览 #
bash
supabase --help
# 主要命令
├── init # 初始化项目
├── start # 启动本地服务
├── stop # 停止本地服务
├── db # 数据库操作
├── migration # 迁移管理
├── functions # Edge Functions
├── gen # 生成类型定义
└── login # 登录Supabase
3.2 登录认证 #
bash
# 登录Supabase账号
supabase login
# 按提示完成浏览器授权
# 登录成功后会保存凭证
# 查看当前登录状态
supabase projects list
3.3 链接远程项目 #
bash
# 链接到远程Supabase项目
supabase link --project-ref <project-ref>
# project-ref 可以在项目设置中找到
# 格式类似: abcdefghijklmnop
四、本地开发环境 #
4.1 初始化项目 #
bash
# 创建项目目录
mkdir my-supabase-app
cd my-supabase-app
# 初始化Supabase
supabase init
# 生成的目录结构
├── supabase/
│ ├── config.toml # 配置文件
│ ├── migrations/ # 迁移文件
│ ├── functions/ # Edge Functions
│ └── seed.sql # 种子数据
└── .gitignore
4.2 配置文件 #
toml
# supabase/config.toml
# 项目配置
[project]
name = "my-supabase-app"
# 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
4.3 启动本地服务 #
bash
# 启动所有服务
supabase start
# 首次启动会拉取Docker镜像,需要等待
# 启动成功后显示
Started supabase local development setup.
API URL: http://localhost:54321
DB URL: postgresql://postgres:postgres@localhost:54322/postgres
Studio URL: http://localhost:54323
Inbucket URL: http://localhost:54324
JWT secret: super-secret-jwt-token-with-at-least-32-characters-long
anon key: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
service_role key: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
4.4 本地服务说明 #
| 服务 | 端口 | 说明 |
|---|---|---|
| API | 54321 | RESTful API |
| Database | 54322 | PostgreSQL |
| Studio | 54323 | 管理界面 |
| Inbucket | 54324 | 邮件测试 |
4.5 停止服务 #
bash
# 停止服务
supabase stop
# 停止并删除数据
supabase stop --no-backup
# 查看服务状态
supabase status
五、IDE配置 #
5.1 VS Code扩展 #
text
推荐扩展
├── Supabase - 官方扩展
├── PostgreSQL - 数据库支持
├── Deno - Edge Functions开发
└── Thunder Client - API测试
5.2 数据库连接配置 #
json
// VS Code settings.json
{
"postgres.credentials": [
{
"host": "localhost",
"port": 54322,
"user": "postgres",
"password": "postgres",
"database": "postgres"
}
]
}
5.3 TypeScript配置 #
json
// tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
六、项目依赖安装 #
6.1 JavaScript/TypeScript #
bash
# 创建项目
npm init -y
# 安装Supabase客户端
npm install @supabase/supabase-js
# 安装开发依赖
npm install -D typescript @types/node
6.2 React项目 #
bash
# 创建React项目
npx create-react-app my-app --template typescript
cd my-app
# 安装Supabase
npm install @supabase/supabase-js @supabase/auth-helpers-react
6.3 Next.js项目 #
bash
# 创建Next.js项目
npx create-next-app@latest my-app --typescript
cd my-app
# 安装Supabase
npm install @supabase/supabase-js @supabase/auth-helpers-nextjs
6.4 Vue项目 #
bash
# 创建Vue项目
npm create vue@latest my-app
cd my-app
# 安装Supabase
npm install @supabase/supabase-js
七、环境变量配置 #
7.1 创建环境变量文件 #
bash
# .env.local (Next.js) 或 .env (其他项目)
# Supabase配置
NEXT_PUBLIC_SUPABASE_URL=http://localhost:54321
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key
# 本地开发使用本地服务
SUPABASE_URL=http://localhost:54321
SUPABASE_ANON_KEY=your-anon-key
SUPABASE_SERVICE_ROLE_KEY=your-service-role-key
7.2 获取密钥 #
bash
# 从本地服务获取
supabase status
# 或从Supabase Dashboard获取
# Settings > API > Project API keys
7.3 安全注意事项 #
bash
# .gitignore 添加
.env
.env.local
.env.*.local
# 永远不要提交密钥到代码仓库
八、验证安装 #
8.1 创建测试文件 #
typescript
// test-connection.ts
import { createClient } from '@supabase/supabase-js'
const supabaseUrl = process.env.SUPABASE_URL!
const supabaseKey = process.env.SUPABASE_ANON_KEY!
const supabase = createClient(supabaseUrl, supabaseKey)
async function testConnection() {
try {
const { data, error } = await supabase.from('_test').select('*').limit(1)
if (error && error.code === '42P01') {
console.log('✅ 连接成功!测试表不存在是正常的。')
} else if (error) {
console.error('❌ 连接失败:', error.message)
} else {
console.log('✅ 连接成功!')
}
} catch (err) {
console.error('❌ 连接异常:', err)
}
}
testConnection()
8.2 运行测试 #
bash
# 使用ts-node运行
npx ts-node test-connection.ts
# 或编译后运行
npx tsc test-connection.ts
node test-connection.js
九、常见问题 #
9.1 Docker相关问题 #
bash
# 确保Docker正在运行
docker ps
# 如果Docker未运行
# macOS: 打开Docker Desktop
# Linux: sudo systemctl start docker
# 重置本地环境
supabase stop --no-backup
supabase start
9.2 端口冲突 #
bash
# 检查端口占用
lsof -i :54321
lsof -i :54322
lsof -i :54323
# 修改config.toml中的端口配置
9.3 CLI更新 #
bash
# 更新CLI
npm update -g supabase
# 或使用Homebrew
brew upgrade supabase
十、总结 #
安装配置完成清单:
| 步骤 | 状态 |
|---|---|
| Node.js安装 | ☐ |
| Supabase CLI安装 | ☐ |
| Docker安装 | ☐ |
| 本地服务启动 | ☐ |
| 项目依赖安装 | ☐ |
| 环境变量配置 | ☐ |
| 连接测试 | ☐ |
下一步,让我们创建第一个Supabase项目!
最后更新:2026-03-28