Seeding the Database with Test Data
Generate realistic test data with INSERT ... SELECT and generate_series, and verify with simple analytical queries.
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));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