How to Seed PostgreSQL with Realistic Test Data
The Problem
PostgreSQL is the world's most advanced open source database, but seeding it with realistic test data is still a manual chore. You end up writing long INSERT statements, or building a script that generates random strings that look nothing like real names, emails, or addresses.
Handling foreign keys makes it worse. You need to insert the parent table first, collect the generated IDs, and then use those when inserting the child table. A single mistyped UUID and your seed script throws a constraint violation.
The Solution: MockHero API
MockHero generates realistic data for any PostgreSQL schema. Define tables, use 156+ field types, connect them with ref fields, and request SQL output to pipe directly into psql. No libraries, no scripts.
Quick Setup
curl -X POST https://api.mockhero.dev/api/v1/generate \
-H "x-api-key: mh_your_api_key" \
-H "Content-Type: application/json" \
-d '{
"tables": [
{
"name": "departments",
"count": 5,
"fields": [
{ "name": "id", "type": "uuid" },
{ "name": "name", "type": "department" },
{ "name": "budget", "type": "decimal", "params": { "min": 50000, "max": 500000 } }
]
},
{
"name": "employees",
"count": 25,
"fields": [
{ "name": "id", "type": "uuid" },
{ "name": "department_id", "type": "ref", "params": { "ref": "departments.id" } },
{ "name": "name", "type": "full_name" },
{ "name": "email", "type": "email" },
{ "name": "salary", "type": "integer", "params": { "min": 40000, "max": 150000 } },
{ "name": "hired_at", "type": "date_past" }
]
}
],
"format": "sql"
}'
Step-by-Step Guide
1. Prerequisites
You need psql and curl installed. Both ship with most Linux and macOS distributions.
2. Get your MockHero API key
Sign up at mockhero.dev/sign-up and copy your key.
3. Create your tables
CREATE TABLE departments (
id UUID PRIMARY KEY,
name TEXT NOT NULL,
budget NUMERIC NOT NULL
);
CREATE TABLE employees (
id UUID PRIMARY KEY,
department_id UUID REFERENCES departments(id),
name TEXT NOT NULL,
email TEXT NOT NULL,
salary INTEGER NOT NULL,
hired_at DATE NOT NULL
);
4. Generate and insert data
Use the curl command from Quick Setup. The format: "sql" option returns INSERT statements you pipe directly into psql.
5. Verify
psql $DATABASE_URL -c "SELECT e.name, d.name AS dept FROM employees e JOIN departments d ON d.id = e.department_id LIMIT 5;"
Complete Example
For a script-based approach, save the curl command to seed.sh and run it whenever you need fresh data. The SQL output mode means zero JavaScript dependencies.
Why MockHero vs Faker / Manual Seeds
- Zero dependencies — SQL output pipes directly into
psql, no Node.js required. - Referential integrity —
reffields ensure every foreign key is valid. - Production-quality values — real-looking names, emails, and dates instead of "test123".
Get Started
Free tier, 1,000 rows/month. Sign up at mockhero.dev and pipe realistic data into your PostgreSQL database today.
MockHero Team
Guides and tutorials for generating realistic test data with the MockHero API.
Start generating test data for free
1,000 rows/month on the free tier. No credit card required.
Get Your API Key