Mock data generation creates artificial records for experimentation, testing, and demonstrations. In PostgreSQL, generate_series supplies rows and identifiers, while random values populate realistic columns. section 1.5
Generate sample stock trades
Create a trades table, then insert 1,000 records with generated IDs, random buyer IDs from 1 through 10, randomly selected symbols, quantities from 1 through 20, prices from 10.00 through 20.00, and the current timestamp. Querying the first five rows verifies the generated output. section 1.5
Build increasingly realistic records by combining simple SQL expressions:
- Generate integers with generate_series(1,5), optionally aliasing the output as id. section 1.5
- Generate random integer fields with random(1,10), or use the earlier-version floor(random() * ...) alternative. section 1.5
- Choose categorical text values by indexing an array such as ['AAPL','F','DASH']. section 1.5
Listing 1.7 Inserting 1,000 sample trades
INSERT INTO trades (id, buyer_id, symbol,
order_quantity, bid_price, order_time)
SELECT
id,
random(1,10) as buyer_id,
(array['AAPL','F','DASH'])[random(1,3)] as symbol,
random(1,20) as order_quantity,
round(random(10.00,20.00), 2) as bid_price,
now() as order_time
FROM generate_series(1,1000) AS id;
Inspect the complete insert query for producing 1,000 sample trades. section 1.5
