Database Migrations
How to manage schema migrations for your Supabase project.
Database migrations are SQL statements that create, update, or delete your existing database schemas. They are a common way of tracking changes to your database over time.
Schema migrations#
For this guide, we'll create a table called employees and see how we can make changes to it.
You will need to install the Supabase CLI and start the local development stack.
If a lock timeout error occurs, in your migration file, consider increasing your lock_timeout setting.
To get started, generate a new migration to store the SQL needed to create our employees table.
1supabase migration new create_employees_tableThis creates a new migration file in supabase/migrations directory.
To that file, add the SQL to create this employees table.
1create table if not exists employees (2 id bigint primary key generated always as identity,3 name text not null,4 email text,5 created_at timestamptz default now()6);Run this migration to create the employees table.
Now you can visit your new employees table in the local Dashboard.
1supabase migration upNext, modify your employees table by adding a column for department.
1supabase migration new add_department_columnTo that new migration file, add the SQL to create a new department column.
1alter table if exists public.employees2add department text default 'Hooli';Run this migration to update your existing employees table.
1supabase migration upFinally, you should see the department column added to your employees table in the local Dashboard.
View the complete code for this example on GitHub.
Seeding data#
Now that you are managing your database with migrations, it would be great have some seed data to use every time you reset the database.
Create a seed script in supabase/seed.sql.
To that file, add the SQL to insert data into your employees table.
1insert into public.employees2 (name)3values4 ('Erlich Bachman'),5 ('Richard Hendricks'),6 ('Monica Hall');Reset your database to reapply migrations and populate with seed data.
1supabase db resetYou should now see the employees table, along with your seed data in the Dashboard! All of your database changes are captured in code, and you can reset to a known state at any time, complete with seed data.
Diffing changes#
This workflow is great if you know SQL and are comfortable creating tables and columns. If not, you can still use the Dashboard to create tables and columns, and then use the CLI to diff your changes and create migrations.
Only use the Dashboard to make schema changes on your local database, then capture them with supabase db diff. Making schema changes directly on your remote database (via the SQL editor or Table Editor) bypasses the migration history and will cause db push to fail with sync errors. Once you're using migrations, all schema changes to your remote database should go through migration files only.
Create a new table called cities, with columns id, name and population.
Then generate a schema diff.
1supabase db diff -f create_cities_tableA new migration file is created for you.
Alternately, you can copy the table definitions directly from the Table Editor.
1create table "public"."cities" (2 "id" bigint primary key generated always as identity,3 "name" text,4 "population" bigint5);Test your new migration file by resetting your local database.
1supabase db resetThe last step is deploying these changes to a live Supabase project.
Deploy your project#
You've been developing your project locally, making changes to your tables via migrations. It's time to deploy your project to the Supabase Platform and start scaling up to millions of users!
Head over to Supabase and create a new project to deploy to.
Login to the Supabase CLI using an auto-generated Personal Access Token.
1supabase loginLink to your remote project by selecting from the on-screen prompt.
1supabase linkPush your migrations and seed the remote database.
1supabase db push --include-seedVisiting your live project on Supabase, you'll see a new employees table, complete with the department column you added in the second migration above.
Working with a team#
When multiple developers share a Supabase project, a few rules keep migrations from getting out of sync.
The golden rule: never change the remote database directly. Once you're using migrations, all schema changes — even small ones — should go through migration files. Using the Dashboard's SQL editor or Table Editor on your remote database bypasses the migration history, and db push will start failing with sync errors.
The team workflow:
Each developer creates migration files on their own branch, never touching the remote database directly.
1supabase migration new your_change_descriptionReset your local database to apply the migration, then commit the migration file to git.
1supabase db reset2git add supabase/migrations3git commit -m "add migration: your_change_description"After pulling new migration files from git, reset your local database to apply them.
1git pull2supabase db resetCoordinate so only one person runs db push at a time. Migration files are applied in timestamp order, so concurrent pushes from different machines can cause conflicts.
1supabase db pushFor a more automated deployment approach, consider using Supabase Branching or a CI/CD pipeline that runs supabase db push on merge to your main branch.
Diagnosing and fixing sync errors#
If db push fails with errors suggesting you run supabase migration repair, your local migration files and the remote database's migration history are out of sync. Here's how to diagnose and fix it.
How migration tracking works#
Supabase tracks which migrations have been applied on each database in a table called supabase_migrations.schema_migrations. When you run supabase db push, it compares your local supabase/migrations folder against that table and runs only the ones not yet applied, in order.
Git tracks your migration files. Supabase tracks what's been applied to each database. These are two separate systems that need to stay in sync.
Step 1: Check what's out of sync#
Start by listing the migration status across local and remote:
1supabase migration listThis shows which migrations are applied locally, which are applied on the remote, and where they diverge.
Step 2: If you made changes on the remote database directly#
Pull the current remote state into a migration file to get back in sync:
1supabase db pullThis creates a new migration file capturing the current remote schema. Commit it to git, then follow the standard workflow going forward.
Step 3: If the migration history table is wrong#
If a migration shows as missing in the remote history table but the schema change is already there (for example, it was applied manually), you can mark it as applied without re-running it:
1supabase migration repair --status applied <migration-timestamp>Or if a migration is recorded as applied but was never run:
1supabase migration repair --status reverted <migration-timestamp>migration repair updates the tracking table only — it does not apply or revert any SQL. Use it to correct the history record when you know the actual database state is correct.