Lyrebird Community 0.5.8 is free · Generate a synthetic test database on your own machine · Download

PostgreSQL Test Data Generation with generate_series

Generate 1,000 customers and 12,000 orders in PostgreSQL with generate_series. Keep foreign keys valid, reproduce a skewed workload, and verify the rows with SQL.

On this page
  1. Generate customers and orders
  2. Verify the row counts and workload
  3. Use the seed in an application test
  4. Try a larger relational schema with Pagila

You can generate repeatable PostgreSQL test data with generate_series() and a couple of INSERT … SELECT statements. The example below creates 1,000 fictional customers and 12,000 orders. Every order points to an existing customer, and 100 customers account for 70% of the orders. That skew gives filters, joins, and pagination more to work with than evenly dividing the orders between customers.

Generate customers and orders

Run this in one session connected to a disposable PostgreSQL test database. The tables are temporary and disappear at commit. Leave the transaction open until you have run the checks.

BEGIN;

CREATE TEMP TABLE seed_customers (
    customer_id integer PRIMARY KEY,
    email text NOT NULL UNIQUE
) ON COMMIT DROP;

CREATE TEMP TABLE seed_orders (
    order_id integer PRIMARY KEY,
    customer_id integer NOT NULL REFERENCES seed_customers (customer_id),
    placed_at timestamp NOT NULL,
    status text NOT NULL CHECK (status IN ('paid', 'pending', 'refunded'))
) ON COMMIT DROP;

INSERT INTO seed_customers (customer_id, email)
SELECT n, 'customer-' || n || '@example.test'
FROM generate_series(1, 1000) AS series(n);

INSERT INTO seed_orders (order_id, customer_id, placed_at, status)
SELECT n,
       CASE WHEN n <= 8400 THEN 1 + (n - 1) % 100
            ELSE 101 + (n - 8401) % 900
       END,
       timestamp '2026-01-01' + (n % 90) * interval '1 day',
       CASE WHEN n % 20 = 0 THEN 'refunded'
            WHEN n % 10 = 0 THEN 'pending'
            ELSE 'paid'
       END
FROM generate_series(1, 12000) AS series(n);

The customer insert runs first so PostgreSQL can enforce the foreign key during the order insert. The modulo expressions assign orders to known customer IDs and use a fixed date range. Running the script again in a fresh session produces the same rows. PostgreSQL documents the inclusive sequence bounds and the constraints applied on insert. Series functions · Constraints

Verify the row counts and workload

The first query should return 1000, 12000, 8400, and 0, in that order. The second should return 10,800 paid orders, 600 pending orders, and 600 refunded orders.

SELECT (SELECT count(*) FROM seed_customers) AS customers,
       count(*) AS orders,
       count(*) FILTER (WHERE o.customer_id <= 100) AS orders_for_top_group,
       count(*) FILTER (WHERE c.customer_id IS NULL) AS missing_customers
FROM seed_orders AS o
LEFT JOIN seed_customers AS c USING (customer_id);

SELECT status, count(*) AS orders
FROM seed_orders
GROUP BY status
ORDER BY status;

COMMIT;

Test a customer from each group. Customer 1 has 84 orders and customer 101 has four. A pagination test with a page size of 20 can check that the first customer has multiple pages and the second has one. For a scenario such as a cancelled order with a failed payment, create that exact fixture in your application’s test setup so the assertion can identify it independently of bulk data.

Use the seed in an application test

Temporary tables live in the connection that created them. For tests that connect through an application pool, apply your actual migrations to a disposable test database and adapt the inserts to the migrated tables. Create one database per parallel worker if workers mutate shared state. Record the migration revision and seed script version so a failing run can be recreated.

For query performance tests, run ANALYZE after loading the data, then exercise the filters and joins that matter to the application. Compare rows per customer, date ranges, and rare statuses with the workload you want to test. Generating more rows alone cannot reproduce a workload whose distribution assumptions are wrong.

Try a larger relational schema with Pagila

This SQL seed works well when you can describe the relationships and value ranges for a small schema. A larger schema can add many-to-many relationships, partitions, or reference tables. If you want a worked example before writing the rules by hand, open our PostgreSQL Pagila sample. It has 49,636 synthetic rows across 22 tables. We checked 42 foreign keys and found zero broken references.

You can download the sealed profile on that page and generate a copy with Lyrebird Community. This edition generates up to 5,000 rows per table, so some tables will be smaller than the full sample shown in the browser. To evaluate the copy against your own test cases, use the synthetic data validation checklist.