liveBook · Manning

Mock data generation

As explained in Just Use Postgres!

Mock data generation is the creation of artificial records that approximate real data for experimentation, testing, or demonstration.

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

How Just Use Postgres! covers it

How deep it goes
Demonstrates generation, randomization, insertion, and verification, while stopping short of advanced data modeling.
Examples
Uses trade identifiers, buyer IDs, stock symbols, quantities, bid prices, timestamps, and a 1,000-row trades table.
What it assumes
Assumes basic SQL and PostgreSQL table familiarity while introducing generate_series through simple queries.
What you can do after
Build it from scratch by writing SQL that generates, inserts, and inspects varied sample rows.
How it connects
Connects generate_series with random number generation, arrays, SQL INSERT statements, and PostgreSQL tables.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.