Database migrations are SQL statements that create, update, or delete database schemas. They provide a structured way to track and version your database changes over time, making it easy to share schema updates across your team and deploy to production.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/supabase/supabase/llms.txt
Use this file to discover all available pages before exploring further.
Why Use Migrations?
Migrations provide several key benefits:- Version control: Track database schema in Git alongside your code
- Reproducibility: Recreate your database schema at any point in time
- Team collaboration: Share schema changes easily across developers
- Safe deployments: Apply tested changes to production systematically
- Rollback capability: Revert changes if issues arise
- Environment parity: Keep development, staging, and production in sync
Migration Workflow
Prerequisites
Ensure you have:- Supabase CLI installed
- A project initialized with
supabase init - Local services running with
supabase start
Creating Your First Migration
Let’s create a simpleemployees table to demonstrate the migration workflow.
Generate a migration file
Create a new migration with a descriptive name:This creates a timestamped file in
supabase/migrations/:Add SQL to your migration
Open the migration file and add your SQL:
supabase/migrations/20240304120000_create_employees_table.sql
Modifying Tables
Now let’s add adepartment column to demonstrate schema evolution:
Schema Diffing
If you prefer using the Dashboard or SQL editor to make changes, you can generate migrations from schema differences.Create Changes in Dashboard
- Open Studio at
http://localhost:54323 - Create a new table called
citieswith columns:id(bigint, primary key, identity)name(text)population(bigint)
Generate Migration from Diff
Generate a migration file from your changes:supabase/migrations/20240304140000_create_cities_table.sql
Test Your Migration
Reset your database to test the migration:- Drop the database
- Reapply all migrations in order
- Run your seed file (if it exists)
Migration Best Practices
1. Keep Migrations Atomic
Each migration should focus on a single logical change:2. Use Idempotent SQL
Make migrations safe to run multiple times:3. Handle Lock Timeouts
For large tables, set lock timeout to prevent blocking:4. Always Enable RLS
Enable Row Level Security on new tables:5. Use Descriptive Names
Name migrations clearly:Common Migration Patterns
Adding Indexes
Use
CONCURRENTLY to avoid locking the table during index creation.Creating Triggers
Adding Foreign Keys
Creating Functions
Renaming Columns Safely
Seeding Data
Usesupabase/seed.sql for development data:
supabase/seed.sql
Deploying Migrations
Link to Remote Project
First, authenticate and link to your Supabase project:Push Migrations to Production
Deploy your migrations:Push with Seed Data (Optional)
For staging environments, you can include seed data:Migration Commands Reference
| Command | Description |
|---|---|
supabase migration new <name> | Create a new migration file |
supabase migration up | Apply pending migrations |
supabase migration list | List all migrations |
supabase migration repair <version> | Mark a migration as applied |
supabase db diff | Show schema differences |
supabase db diff -f <name> | Create migration from diff |
supabase db reset | Reset database and reapply migrations |
supabase db push | Push migrations to remote database |
Troubleshooting
Migration fails with syntax error
Migration fails with syntax error
Error:
syntax error at or near...Solution:- Validate SQL syntax in a SQL editor
- Check for missing semicolons
- Ensure proper quoting of identifiers
- Test migration locally first:
supabase db reset
Table already exists
Table already exists
Error:
relation "table_name" already existsSolution:- Use
CREATE TABLE IF NOT EXISTS - Check if migration was already applied
- Review migration history:
supabase migration list
Lock timeout exceeded
Lock timeout exceeded
Error:
canceling statement due to lock timeoutSolution:- Increase lock timeout in migration:
- Run during low-traffic period
- Use
CONCURRENTLYfor index creation
Permission denied
Permission denied
Error:
permission denied for schema publicSolution:- Ensure you’re using the correct schema
- Check RLS policies aren’t blocking operations
- Verify migration uses proper privileges
Advanced Topics
Multi-Stage Deployments
For zero-downtime deployments of breaking changes:Managing Multiple Environments
Use different projects for each environment:Next Steps
Deploy to Production
Learn how to deploy your project to Supabase Platform
Database Testing
Set up automated testing for your database
CI/CD Integration
Automate migrations with GitHub Actions
CLI Reference
Complete CLI command reference
