Learn how to seed data in Supabase. This guide walks you through setting up your project, installing the CLI, creating seed files, and verifying your data insertion.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set Up Your Supabase Project
First, you'll need to have a Supabase project set up. If you haven't already:
Step 2: Install Supabase CLI
To seed data, you'll need to use the Supabase CLI. Install it using npm:
npm install -g supabase
Step 3: Set Up Your Local Environment
supabase init
Step 4: Create Your Seed Data File
Create a SQL file with your seed data. This file will contain the SQL commands needed to insert data into your database tables.
Save this file in a directory within your project, for example, db/seeds.sql
.
Example content for seeds.sql
:
-- Example seed data
INSERT INTO profiles (id, username, email) VALUES
(1, 'user1', '[email protected]'),
(2, 'user2', '[email protected]');
Step 5: Prepare Supabase for Seeding
Before seeding data, ensure your database is running and your environment is correctly set up. You might need to run Supabase locally for testing:
supabase start
This will start your Supabase local development environment.
Step 6: Seed the Database
Run the supabase db reset
command followed by --file
to specify your seed file. This will drop all tables and apply the seed data.
supabase db reset --file db/seeds.sql
This command will execute your seeds.sql
file, inserting the specified data into your database tables.
Step 7: Verify Your Seeded Data
After running the seed script, you should validate that the data was inserted correctly:
Step 8: Clean Up and Stop Local Environment
Once you've completed the data seeding and verification, you can stop the local Supabase environment if it's running:
supabase stop
This stops your local Supabase development server, freeing up resources on your machine.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.