0
0
Supabasecloud~5 mins

Creating migrations in Supabase

Choose your learning style9 modes available
Introduction

Migrations help you save and track changes to your database structure. They make it easy to update your database safely and share changes with your team.

When you add a new table to store data.
When you change a column type or add a new column.
When you remove or rename a table or column.
When you want to keep a history of database changes.
When working with a team to keep everyone's database in sync.
Syntax
Supabase
supabase migration new <migration_name>
supabase db reset
supabase migration apply

supabase migration new creates a new migration file with your changes.

supabase db reset resets the local database and applies migrations.

supabase migration apply applies migrations to the remote database.

Examples
This creates a migration file to add a users table.
Supabase
supabase migration new add_users_table
This resets the local database and applies all migrations.
Supabase
supabase db reset
This applies migrations to your remote Supabase database.
Supabase
supabase migration apply
Sample Program

This migration creates a new table called users with three columns: id, name, and email. The commands create the migration file, reset and apply it locally, and then apply it to the remote database.

Supabase
-- Migration file: 20240601_add_users_table.sql
CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  name TEXT NOT NULL,
  email TEXT UNIQUE NOT NULL
);

-- Commands to run:
supabase migration new add_users_table
supabase db reset
supabase migration apply
OutputSuccess
Important Notes

Always write clear and descriptive migration names.

Test migrations locally before applying them to production.

Keep migrations small and focused on one change at a time.

Summary

Migrations save and track database changes.

Use supabase migration new to create migration files.

Reset and apply migrations locally with supabase db reset and apply migrations remotely with supabase migration apply.