generate_series is a PostgreSQL set-returning function for producing sequential integers or timestamps across an inclusive range. Its optional step defaults to 1. section 1.5
Generate mock stock trades
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; Use the generated rows directly in an INSERT to populate a trades table with sample data. 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;
Shows a complete INSERT query that combines generated identifiers with random buyers, symbols, quantities, prices, and timestamps. section 1.5
