15. Local Development, CLI & Migrations
Production-Grade Supabase Development Workflow
The Supabase CLI gives you a complete local development environment — a full Docker-based stack with Postgres, Auth, Storage, Edge Functions, and the Studio UI — all running on your machine. Paired with a migration-based schema workflow, it enables safe, reproducible deployments to production.
Local development is essential for professional engineering. It allows you to test changes quickly without affecting production data. The Supabase CLI provides a complete stack that mirrors your production environment. You can develop features locally, test them thoroughly, and then deploy with confidence. Migrations are the key to managing schema changes. Each migration is a SQL file with a timestamp prefix. Migrations are applied in order, ensuring consistent schema across environments. The CLI also generates TypeScript types from your schema, providing end-to-end type safety. The local development workflow is the foundation of professional Supabase development. It ensures that changes are tested before they reach production.
CLI Setup & Local Stack
Install the Supabase CLI with npm. Initialize your project with supabase init — this creates a supabase/ directory. Start the full stack with supabase start — this pulls Docker images and runs the complete Supabase stack. The local stack includes Postgres (port 54322), the API (port 54321), Auth, Storage, and Studio (port 54323). You can connect with psql or any Postgres client. The local stack uses the same code as production, so it's reliable. Stop the stack with supabase stop. Check status with supabase status. The CLI also handles linking to remote projects. This local development environment is essential for testing features, debugging issues, and writing migrations.
# Install CLI npm install -g supabase # Initialize project supabase init # Start the full local stack supabase start # Output: # API URL: http://localhost:54321 # Studio URL: http://localhost:54323 # DB URL: postgresql://postgres:postgres@localhost:54322/postgres
Migrations: Schema Version Control
Migrations are SQL files that define your schema changes. Create a new migration with supabase migration new. This creates a file with a timestamp prefix in supabase/migrations/. Write your SQL in the file — table definitions, indexes, policies, etc. Apply migrations locally with supabase db push. Apply to production with supabase db push --linked. Generate a migration from schema diff with supabase db diff. Reset your local DB with supabase db reset — this drops and recreates the database, then replays all migrations. Never edit a migration file after it's been applied — create a new one instead. This ensures your migration history is immutable and reproducible. Commit migration files to git. This is the standard practice for professional database development.
# Create a new migration file supabase migration new add_posts_table # Creates: supabase/migrations/20241015120000_add_posts_table.sql # Apply migrations to local DB supabase db push # Apply to remote (production) supabase db push --linked
Database Seed Files
Seed files populate your local database with test data. The seed.sql file runs automatically after supabase db reset. It's used to provide initial data for development and testing. Seed files should include realistic data that mimics production. This helps you test features with real-world data. For authentication, you may need to create test users. For multi-tenant apps, create orgs and memberships. Seed files are not applied to production — they're only for development. They're essential for a productive development workflow. You can also create additional seed files for specific scenarios. The seed file should be idempotent — it should work even if run multiple times. Use ON CONFLICT DO NOTHING to avoid duplicate errors.
-- supabase/seed.sql (runs after db reset) INSERT INTO public.profiles (id, full_name, role) VALUES ('00000000-0000-0000-0000-000000000001', 'Alice Admin', 'admin'), ('00000000-0000-0000-0000-000000000002', 'Bob User', 'user');
Type Generation
The Supabase CLI can generate TypeScript types from your database schema. Run supabase gen types typescript --linked > src/types/database.types.ts. This creates a type definition file with all your tables, columns, and functions. Import this file into your application and use it with the Supabase client. This provides end-to-end type safety — if you rename a column, the TypeScript compiler will catch all usages. The generated types include Row (SELECT result), Insert (INSERT data), and Update (UPDATE data) types. This is the single most effective way to prevent errors in your application code. Always generate types after schema changes. This practice catches issues at compile time instead of runtime.
// Generate TypeScript types supabase gen types typescript --linked > src/types/database.types.ts // Use in your app import { Database } from '@/types/database.types'; const supabase = createClient<Database>(url, key); // TypeScript now knows the shape of 'posts' const { data } = await supabase.from('posts').select('id, title').single();
Knowledge Check
Ready to test your understanding of 15. Local Development, CLI & Migrations?