Setting Up Supabase Locally
This guide will help you set up a local Supabase instance for development.
Prerequisites
- Docker installed and running
- Project cloned and dependencies installed
Steps
-
Navigate to Supabase Package
cd packages/supabase
-
Start Supabase Services
npx supabase start
This command will:
- Pull required Docker images
- Start the Supabase services
- Apply migrations
- Generate types
-
Verify the Setup
Once started, you can access:
- Supabase Studio: http://localhost:54323
- Database:
postgresql://postgres:postgres@localhost:54322/postgres
- API URL:
http://localhost:54321
-
Configure Environment Variables
Copy the environment variables from your local Supabase instance:
# From packages/supabase
cp .env.example .envUpdate
.env
with your local Supabase credentials:NEXT_PUBLIC_SUPABASE_URL=http://localhost:54321
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key
SUPABASE_SERVICE_ROLE_KEY=your-service-role-keyYou can find these values in the output of
npx supabase start
or in Supabase Studio.
Development Workflow
-
Start Development Environment
# From project root
pnpm dev -
Create and Apply Migrations
When making database changes:
# Create a new migration
npx supabase migration new my_change
# Apply migrations
npx supabase db reset -
Generate Types
After schema changes:
pnpm generate:types
Common Commands
All commands should be run from packages/supabase
:
# Start Supabase
npx supabase start
# Stop Supabase
npx supabase stop
# Reset database
npx supabase db reset
# View database changes
npx supabase db diff
# Create new migration
npx supabase migration new my_migration
Troubleshooting
-
Docker Issues
- Ensure Docker is running
- Try stopping and removing containers:
npx supabase stop
docker-compose down
-
Database Connection Issues
- Check if ports 54321-54323 are available
- Verify Docker network settings
- Ensure environment variables are correct
-
Migration Problems
- Reset the database:
npx supabase db reset
- Check migration files for syntax errors
- Verify migration order
- Reset the database: