Files
supabase/apps/docs/content/guides/deployment/database-migrations.mdx
T
2026-01-14 07:56:25 +00:00

346 lines
8.9 KiB
Plaintext

---
id: 'database-migrations'
title: 'Database Migrations'
description: 'How to manage schema migrations for your Supabase project.'
subtitle: 'How to manage schema migrations for your Supabase project.'
video: 'https://www.youtube-nocookie.com/v/Kx5nHBmIxyQ'
tocVideo: 'Kx5nHBmIxyQ'
---
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](/docs/guides/local-development#quickstart) the Supabase CLI and start the local development stack.
<Admonition type="tip">
If a lock timeout error occurs, in your migration file, consider increasing your [`lock_timeout`](https://postgresqlco.nf/doc/en/param/lock_timeout/) setting.
</Admonition>
<StepHikeCompact>
<StepHikeCompact.Step step={1}>
<StepHikeCompact.Details title="Create your first migration file">
To get started, generate a [new migration](/docs/reference/cli/supabase-migration-new) to store the SQL needed to create our `employees` table.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```bash name=Terminal
supabase migration new create_employees_table
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
</StepHikeCompact>
<StepHikeCompact>
<StepHikeCompact.Step step={2}>
<StepHikeCompact.Details title="Add the SQL to your migration file">
This creates a new migration file in supabase/migrations directory.
To that file, add the SQL to create this `employees` table.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```sql name=supabase/migrations/<timestamp>_create_employees_table.sql
create table if not exists employees (
id bigint primary key generated always as identity,
name text not null,
email text,
created_at timestamptz default now()
);
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
</StepHikeCompact>
<StepHikeCompact>
<StepHikeCompact.Step step={3}>
<StepHikeCompact.Details title="Apply your first migration">
Run this migration to create the `employees` table.
Now you can visit your new `employees` table in the local Dashboard.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```bash name=Terminal
supabase migration up
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
</StepHikeCompact>
<StepHikeCompact>
<StepHikeCompact.Step step={4}>
<StepHikeCompact.Details title="Modify your employees table">
Next, modify your `employees` table by adding a column for `department`.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```bash name=Terminal
supabase migration new add_department_column
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
</StepHikeCompact>
<StepHikeCompact>
<StepHikeCompact.Step step={5}>
<StepHikeCompact.Details title="Add a new column to your table">
To that new migration file, add the SQL to create a new `department` column.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```sql name=supabase/migrations/<timestamp>_add_department_column.sql
alter table if exists public.employees
add department text default 'Hooli';
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
</StepHikeCompact>
<StepHikeCompact>
<StepHikeCompact.Step step={6}>
<StepHikeCompact.Details title="Apply your second migration">
Run this migration to update your existing `employees` table.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```bash name=Terminal
supabase migration up
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
</StepHikeCompact>
Finally, you should see the `department` column added to your `employees` table in the local Dashboard.
<Admonition type="note">
View the [complete code](https://github.com/supabase/supabase/tree/master/examples/database/employees) for this example on GitHub.
</Admonition>
### Seeding data
Now that you are managing your database with migrations scripts, it would be great have some seed data to use every time you reset the database.
<StepHikeCompact>
<StepHikeCompact.Step step={1}>
<StepHikeCompact.Details title="Populate your table">
Create a seed script in supabase/seed.sql.
To that file, add the SQL to insert data into your `employees` table.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```sql name=supabase/seed.sql
insert into public.employees
(name)
values
('Erlich Bachman'),
('Richard Hendricks'),
('Monica Hall');
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
</StepHikeCompact>
<StepHikeCompact>
<StepHikeCompact.Step step={2}>
<StepHikeCompact.Details title="Reset your database">
Reset your database to reapply migrations and populate with seed data.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```bash name=Terminal
supabase db reset
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
</StepHikeCompact>
You 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.
<StepHikeCompact>
<StepHikeCompact.Step step={1}>
<StepHikeCompact.Details title="Create your table from the Dashboard">
Create a new table called `cities`, with columns `id`, `name` and `population`.
Then generate a [schema diff](/docs/reference/cli/supabase-db-diff).
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```bash name=Terminal
supabase db diff -f create_cities_table
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
</StepHikeCompact>
<StepHikeCompact>
<StepHikeCompact.Step step={2}>
<StepHikeCompact.Details title="Add schema diff as a migration">
A new migration file is created for you.
Alternately, you can copy the table definitions directly from the Table Editor.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```sql name=supabase/migrations/<timestamp>_create_cities_table.sql
create table "public"."cities" (
"id" bigint primary key generated always as identity,
"name" text,
"population" bigint
);
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
</StepHikeCompact>
<StepHikeCompact>
<StepHikeCompact.Step step={3}>
<StepHikeCompact.Details title="Test your migration">
Test your new migration file by resetting your local database.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```bash name=Terminal
supabase db reset
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
</StepHikeCompact>
The 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](/dashboard) and create a new project to deploy to.
<StepHikeCompact>
<StepHikeCompact.Step step={1}>
<StepHikeCompact.Details title="Log in to the Supabase CLI">
[Login](/docs/reference/cli/supabase-login) to the Supabase CLI using an auto-generated Personal Access Token.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```bash name=Terminal
supabase login
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
</StepHikeCompact>
<StepHikeCompact>
<StepHikeCompact.Step step={2}>
<StepHikeCompact.Details title="Link your project">
[Link](/docs/reference/cli/supabase-link) to your remote project by selecting from the on-screen prompt.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```bash name=Terminal
supabase link
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
</StepHikeCompact>
<StepHikeCompact>
<StepHikeCompact.Step step={3}>
<StepHikeCompact.Details title="Deploy database migrations">
[Push](/docs/reference/cli/supabase-db-push) your migrations to the remote database.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```bash name=Terminal
supabase db push
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
</StepHikeCompact>
<StepHikeCompact>
<StepHikeCompact.Step step={4}>
<StepHikeCompact.Details title="Deploy database seed data (optional)">
[Push](/docs/reference/cli/supabase-db-push) your migrations and seed the remote database.
</StepHikeCompact.Details>
<StepHikeCompact.Code>
```bash name=Terminal
supabase db push --include-seed
```
</StepHikeCompact.Code>
</StepHikeCompact.Step>
</StepHikeCompact>
Visiting your live project on [Supabase](/dashboard/project/_), you'll see a new `employees` table, complete with the `department` column you added in the second migration above.