📖 Scenario: You are setting up a new Supabase project for a small online bookstore. To get started, you need to add some initial books data into your database so the app can display books right away.
🎯 Goal: Create seed data for the books table in Supabase, configure a helper variable for batch insertion, write the main logic to insert the seed data, and finalize the seed script for deployment.
📋 What You'll Learn
Create a list of book records with exact fields and values
Add a configuration variable for batch size
Write the insertion logic using Supabase client
Complete the seed script with proper export and async function
💡 Why This Matters
🌍 Real World
Seeding initial data is a common step when setting up new cloud databases to have ready-to-use content for development or demos.
💼 Career
Knowing how to manage seed data with Supabase is useful for backend developers and cloud engineers working with modern serverless databases.
Progress0 / 4 steps
1
Create the initial seed data array
Create a constant called books that is an array of objects. Each object must have these exact fields and values: { id: 1, title: 'The Great Gatsby', author: 'F. Scott Fitzgerald', year: 1925 }, { id: 2, title: '1984', author: 'George Orwell', year: 1949 }, and { id: 3, title: 'To Kill a Mockingbird', author: 'Harper Lee', year: 1960 }.
Supabase
Hint
Use const books = [ ... ] and include exactly three objects with the given fields and values.
2
Add a batch size configuration variable
Create a constant called BATCH_SIZE and set it to 3 to control how many records to insert at once.
Supabase
Hint
Use const BATCH_SIZE = 3; exactly as shown.
3
Write the seed insertion logic
Write an async function called seedBooks that uses the Supabase client supabase to insert the books array into the books table using supabase.from('books').insert(books). Await the insertion call.
Supabase
Hint
Define async function seedBooks() and inside it call await supabase.from('books').insert(books);
4
Complete the seed script for deployment
Export the seedBooks function as the default export using export default seedBooks;.
Supabase
Hint
Use export default seedBooks; exactly as shown to complete the seed script.
Practice
(1/5)
1. What is the main purpose of seed data in a Supabase project?
easy
A. To delete all existing data from the database
B. To backup the database before changes
C. To add initial data automatically to the database
D. To monitor database performance
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 C
Quick 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
A. supabase db backup create
B. supabase db migrate run
C. supabase db reset
D. supabase db seed run
Solution
Step 1: Recall Supabase seed command
The command to execute seed scripts is supabase db seed run.
Step 2: Differentiate from other commands
supabase db migrate run applies migrations, not seeds; reset and backup are unrelated.
Final Answer:
supabase db seed run -> Option D
Quick Check:
Seed scripts run with supabase 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
A. 3
B. 2
C. 1
D. 4
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 A
Quick 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
A. Database connection is lost
B. Seed script tries to insert data with existing primary keys
C. Supabase CLI is not installed
D. Seed script syntax is incorrect
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 B
Quick Check:
Duplicate key error = duplicate primary keys in seed [OK]
Hint: Duplicate key error means repeated primary keys in seed data [OK]