0
0
Supabasecloud~5 mins

Running migrations locally and remotely in Supabase

Choose your learning style9 modes available
Introduction

Migrations help you change your database structure safely. Running them locally lets you test changes. Running them remotely updates your live database.

You want to add a new table or column to your database.
You need to fix a mistake in your database structure before going live.
You want to keep your local database and remote database in sync.
You are preparing a new feature that requires database changes.
You want to roll back a change if something goes wrong.
Syntax
Supabase
supabase migration new <migration_name>
supabase db reset
supabase db push

supabase migration new creates a new migration file.

supabase db reset resets your local database and applies migrations.

Examples
This creates a new migration file named add_users_table where you define your changes.
Supabase
supabase migration new add_users_table
This command resets your local database and runs all pending migrations.
Supabase
supabase db reset
This applies your migrations to the remote Supabase database.
Supabase
supabase db push
Sample Program

First, create a migration to add an email column. Then apply it locally to test. Finally, push the change to the remote database.

Supabase
supabase migration new add_email_to_users
-- Edit the migration file to add a new column 'email' to 'users' table
supabase db reset
supabase db push
OutputSuccess
Important Notes

Always test migrations locally before applying them remotely to avoid breaking your live app.

Keep your migration files in version control to track changes over time.

Use clear and descriptive names for migration files to remember their purpose.

Summary

Migrations change your database structure safely.

Run migrations locally first to test changes.

Run migrations remotely to update your live database.