You can generate test data in PostgreSQL without any extra tool. The built-in generate_series() function produces as many rows as you ask for, and random() fills them with varied values. With a few patterns you can keep foreign keys valid, make the data reproducible, and give it the skew of real data. This guide shows those patterns, then the tools to use when you need more.
A schema to fill
CREATE TABLE customers ( customer_id INT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE, country TEXT NOT NULL, created_at TIMESTAMPTZ NOT NULL);CREATE TABLE products ( product_id INT PRIMARY KEY, name TEXT NOT NULL, category TEXT NOT NULL, price NUMERIC(10,2) NOT NULL);CREATE TABLE orders ( order_id INT PRIMARY KEY, customer_id INT NOT NULL REFERENCES customers (customer_id), order_date DATE NOT NULL, status TEXT NOT NULL);CREATE TABLE order_lines ( order_id INT NOT NULL REFERENCES orders (order_id), line_number INT NOT NULL, product_id INT NOT NULL REFERENCES products (product_id), quantity INT NOT NULL, PRIMARY KEY (order_id, line_number));Generate parent tables with generate_series
SELECT setseed(0.42); -- same "random" data every runINSERT INTO customersSELECT i, 'Customer ' || i, 'user' || i || '@example.com', (ARRAY['US','GB','DE','IN','BR'])[1 + floor(random() * 5)::int], now() - random() * interval '730 days'FROM generate_series(1, 10000) AS i;INSERT INTO productsSELECT i, 'Product ' || i, (ARRAY['Coffee','Tea','Equipment','Gifts'])[1 + floor(random() * 4)::int], round((3 + random() * 97)::numeric, 2)FROM generate_series(1, 500) AS i;Patterns used here:
generate_series(1, N)gives one row per number, which doubles as the primary key.(ARRAY[...])[1 + floor(random() * n)::int]picks a random value from a list.now() - random() * interval '730 days'spreads timestamps over two years.setseed()makesrandom()repeatable in the same session, so tests are stable.
Keep foreign keys valid
Because parent keys run from 1 to N, a child row can pick a valid parent with 1 + floor(random() * N)::int:
INSERT INTO ordersSELECT i, 1 + floor(random() * 10000)::int, current_date - floor(random() * 730)::int, (ARRAY['placed','shipped','delivered','refunded'])[1 + floor(random() * 4)::int]FROM generate_series(1, 50000) AS i;For a variable number of children per parent, use a lateral generate_series driven by the parent row:
INSERT INTO order_linesSELECT o.order_id, l.n, 1 + floor(random() * 500)::int, 1 + floor(random() * 5)::intFROM orders oCROSS JOIN LATERAL generate_series(1, 1 + o.order_id % 4) AS l(n);Insert parents before children, in dependency order, and the constraints hold throughout.
Make it look like real data
Uniform random data hides problems that real data exposes. A few adjustments help:
- Skew: real customers are not equally active.
1 + floor(10000 * power(random(), 3))::intpicks low customer ids far more often, giving a long tail. - Nulls:
CASE WHEN random() < 0.1 THEN NULL ELSE ... ENDgives 10% missing values. - Seasonality: weight dates towards certain months, or generate per day with a varying row count.
- Edge cases: add a few rows on purpose, such as orders with no lines, names with apostrophes, and timestamps at midnight on a daylight saving change.
- Volume: test performance with realistic sizes. Wrapping the load in one transaction and adding indexes after inserting is much faster.
Tools for bigger or richer test data
- pgbench ships with PostgreSQL;
pgbench -i -s 10creates and fills its own benchmark tables, useful for load testing but not for your schema. - Faker libraries (Python, JavaScript and others) generate realistic names, addresses and text, which you can load with
COPY. - Subsetting and masking production gives the most realistic data, but personal data must be masked or synthesised first. Never copy real customer data into test environments as-is.
Generate a schema and seed data together
If you are designing the schema as well, offdataai can do both. Describe the database in plain English or start from one of 174 templates, and it generates PostgreSQL DDL with keys and constraints, plus seed SQL that inserts rows in dependency order so every foreign key is valid. The seed rows are a starting point for smoke tests and dbt builds; scale them up with the generate_series patterns above. Seed data is included on the Pro plan. See the PostgreSQL schema generator.
Frequently asked questions
How do I generate a million rows in PostgreSQL?
INSERT INTO t SELECT ... FROM generate_series(1, 1000000) AS i; fills a million rows in seconds on most machines. Create indexes after loading for speed.
How do I generate random data that keeps foreign keys valid?
Generate parent tables first with sequential keys from generate_series, then pick parent keys in child rows with 1 + floor(random() * parent_count)::int.
How do I make random test data reproducible in Postgres?
Call SELECT setseed(0.42); (any value between -1 and 1) in the same session before generating. The same seed produces the same sequence from random().
Is there a fake data generator built into PostgreSQL?
Not as a named tool, but generate_series(), random(), md5() and gen_random_uuid() cover most needs. For realistic names and addresses, use a Faker library and load the output with COPY.
