Seeding the Database with Test Data
Generate realistic test data with INSERT ... SELECT and generate_series, and verify with simple analytical queries.
Seeding the Database with Test Data is a free SQL Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SQL Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Seed?
Empty databases hide bugs. You want realistic data:
- To run UI/E2E tests
- To verify performance characteristics (indexes, query plans)
- To demo the app
Hand-Written Seeds
For a handful of "fixture" rows, write them by hand in SQL or a migration:
INSERT INTO users (id, email, full_name) VALUES
(1, 'alice@example.com', 'Alice Adams'),
(2, 'bob@example.com', 'Bob Brown');
SELECT setval('users_id_seq', (SELECT MAX(id) FROM users));Generated Series for Bulk
generate_series is your friend. Make 1000 posts:
INSERT INTO posts (author_id, title, body, published_at)
SELECT
((random() * 9 + 1)::int), -- random user 1-10
'Post number ' || g,
'Lorem ipsum body ' || g,
NOW() - (random() * INTERVAL '90 days')
FROM generate_series(1, 1000) AS g;Random Foreign Keys
Pick a random parent row:
INSERT INTO comments (post_id, author_id, body)
SELECT
(SELECT id FROM posts ORDER BY random() LIMIT 1),
(SELECT id FROM users ORDER BY random() LIMIT 1),
'Sample comment ' || g
FROM generate_series(1, 5000) AS g;
-- Slow at scale — see next slide for a faster pattern.Faster: Pre-Cache the Parents
Materialise candidate IDs once:
WITH u AS (SELECT id FROM users),
p AS (SELECT id FROM posts)
INSERT INTO comments (post_id, author_id, body)
SELECT
(SELECT id FROM p OFFSET floor(random()*1000)::int LIMIT 1),
(SELECT id FROM u OFFSET floor(random()*10)::int LIMIT 1),
'Sample comment ' || g
FROM generate_series(1, 5000) AS g;faker_fdw and Tools
For richer fake data (names, addresses, paragraphs), use:
faker_fdw— Postgres extension exposing Python Faker- Node's
@faker-js/faker - Django/Rails seeders in scripted form
COPY for Big Bulk Imports
To load a million rows, COPY beats INSERT by 5–10x:
COPY users (email, full_name)
FROM '/tmp/seed_users.csv' WITH (FORMAT csv, HEADER true);Resetting Sequences
If you manually inserted rows with explicit IDs, fast-forward the sequence so future auto-IDs don't collide:
SELECT setval('users_id_seq', (SELECT COALESCE(MAX(id), 0) FROM users));Idempotent Seeds
Make seeds runnable many times safely. Use UPSERT or TRUNCATE-then-INSERT:
INSERT INTO products (sku, name, price) VALUES
('A-001', 'Widget', 9.99)
ON CONFLICT (sku) DO UPDATE
SET name = EXCLUDED.name, price = EXCLUDED.price;Realistic Distribution
Tests should reflect production shape. If 80% of posts have 0 comments and 1% have 1000, the test data should look like that or your index plans will mislead.
Seeding in CI
For end-to-end tests, run the seed script after migrations and before the test suite. Keep seeds in version control.
Recap
Good seeds = realistic data + idempotent script + fast load (COPY for big data).
- generate_series for bulk
- UPSERT for repeated runs
- setval after manual inserts
- Match production distributions
Quick Check
You want to insert 10,000 rows of test data quickly. Which PostgreSQL function generates the row numbers?
Frequently asked questions
Is the “Seeding the Database with Test Data” lesson free?
Yes — the full text of “Seeding the Database with Test Data” is free to read here on the web, and the SQL Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SQL Academy course, upgrade to CoddyKit PRO.
What will I learn in “Seeding the Database with Test Data”?
Generate realistic test data with INSERT ... SELECT and generate_series, and verify with simple analytical queries. You practise SQL Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SQL Academy?
No prior experience is required. SQL Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Seeding the Database with Test Data” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SQL Academy lesson?
Yes. Every SQL Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Modelling a Blog: Users Posts Comments
- Choosing Keys and Types
- Indexes for Common Queries
- Seeding the Database with Test Data