Seed data management helps you add initial data to your database automatically. This makes your app ready to use right after setup.
Seed data management in Supabase
Introduction
Syntax
Supabase
supabase db seed run
This command runs seed scripts that add data to your database.
The seed script is an SQL file named `seed.sql` placed in the `supabase/seed/` folder.
Examples
Supabase
supabase db seed run
Supabase
supabase db seed run --schema=public
Sample Program
This SQL seed file adds two users to the users table. Running supabase db seed run will insert these rows into your database.
Supabase
-- File: supabase/seed/seed.sql INSERT INTO users (id, name, email) VALUES (1, 'Alice', 'alice@example.com'), (2, 'Bob', 'bob@example.com');
Important Notes
Always back up your database before running seed scripts in production.
Seed scripts should be idempotent or carefully managed to avoid duplicate data.
Use seed data to speed up development and testing with consistent data sets.
Summary
Seed data management adds initial data to your database automatically.
Use supabase db seed run to execute seed scripts.
Seed data helps with testing, development, and consistent setups.
Practice
1. What is the main purpose of seed data in a Supabase project?
easy
Solution
Step 1: Understand seed data concept
Seed data is used to populate the database with initial or default data automatically.Step 2: Identify the purpose in Supabase
In Supabase, seed data helps set up consistent data for testing and development.Final Answer:
To add initial data automatically to the database -> Option CQuick Check:
Seed data = initial data setup [OK]
Hint: Seed data means initial data added automatically [OK]
Common Mistakes:
- Confusing seed data with backup or monitoring
- Thinking seed deletes data instead of adding
- Assuming seed data is for performance tuning
2. Which command runs seed scripts in a Supabase project?
easy
Solution
Step 1: Recall Supabase seed command
The command to execute seed scripts issupabase db seed run.Step 2: Differentiate from other commands
supabase db migrate runapplies migrations, not seeds; reset and backup are unrelated.Final Answer:
supabase db seed run -> Option DQuick Check:
Seed scripts run withsupabase db seed run[OK]
Hint: Seed scripts run with 'supabase db seed run' command [OK]
Common Mistakes:
- Using migrate command instead of seed
- Confusing reset with seed run
- Trying backup commands for seeding
3. Given this seed script snippet in Supabase:
INSERT INTO users (id, name) VALUES (1, 'Alice'), (2, 'Bob'); INSERT INTO users (id, name) VALUES (3, 'Charlie');What will be the total number of users after running the seed?
medium
Solution
Step 1: Count inserted rows in first statement
The first INSERT adds 2 users: Alice and Bob.Step 2: Count inserted rows in second statement
The second INSERT adds 1 user: Charlie.Step 3: Calculate total users inserted
Total users = 2 + 1 = 3.Final Answer:
3 -> Option AQuick Check:
2 + 1 = 3 users inserted [OK]
Hint: Add all inserted rows to find total users [OK]
Common Mistakes:
- Counting only first INSERT statement
- Assuming duplicate rows overwrite
- Miscounting number of values per INSERT
4. You run
supabase db seed run but get an error: duplicate key value violates unique constraint. What is the likely cause?medium
Solution
Step 1: Understand the error message
The error means the seed script tries to insert rows with keys already in the database.Step 2: Identify cause in seed data context
Duplicate primary keys cause conflicts when inserting seed data multiple times without cleanup.Final Answer:
Seed script tries to insert data with existing primary keys -> Option BQuick Check:
Duplicate key error = duplicate primary keys in seed [OK]
Hint: Duplicate key error means repeated primary keys in seed data [OK]
Common Mistakes:
- Assuming syntax error causes duplicate key message
- Blaming CLI installation for data errors
- Ignoring database state before seeding
5. You want to seed a Supabase database with initial users but avoid duplicate entries if the seed runs multiple times. Which approach is best?
hard
Solution
Step 1: Understand the problem of duplicates
Running seed multiple times can cause duplicate key errors if data already exists.Step 2: Choose a safe insert method
UsingINSERT ... ON CONFLICT DO NOTHINGskips inserting duplicates safely.Step 3: Evaluate other options
Deleting database or tables is risky and inefficient; running seed once is error-prone.Final Answer:
UseINSERT ... ON CONFLICT DO NOTHINGin seed scripts -> Option AQuick Check:
Safe inserts prevent duplicates with ON CONFLICT [OK]
Hint: Use ON CONFLICT DO NOTHING to avoid duplicate inserts [OK]
Common Mistakes:
- Dropping tables can cause data loss
- Assuming seed runs only once always
- Deleting whole database is overkill
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
