Search code examples
postgresqlrandominsertlarge-data

Insert automatically large amount of records in postgresSQL table


i need to populate my table randomly with large amount of record in PostgresSQL like 200k

CREATE TABLE qr_code.tbl_transaction (
    transaction_id varchar NOT NULL,
    importo numeric NOT NULL,
    alias varchar NOT NULL,
    order_id varchar NOT NULL,
    filiale varchar NOT NULL,
    descrizione varchar NOT NULL,
    data_creazione timestamp NOT NULL,
    terminale varchar NOT NULL,
    data_esecuzione timestamp NULL,
    chiave_movimento_prenotata varchar NULL,
    stato varchar NULL,
    codice_fiscale varchar(16) NULL,
    CONSTRAINT tbl_transaction_pk PRIMARY KEY (transaction_id)
);

How can i do this quickly?


Solution

  • You can use generate_series() to generate a lot of rows and use random() to generate random values.

    Something like:

    insert into tbl_transaction (transaction_id, importo, alias, order_id, filiale, descrizione, data_creazione, terminale, data_esecuzione, chiave_movimento_prenotata, stato, codice_fiscale)
    select g.id::text, 
           random() * 1000,
           'some alias',
           (random()*10000 + 1)::text,
           md5(random()::text),
           md5(random()::text),
           timestamp '2000-01-01' + make_interval(days => (random() * 7500 + 1)::int),
           md5(random()::text),
           timestamp '2000-01-01' + make_interval(days => (random() * 7500 + 1)::int),
           md5(random()::text), 
           case (id % 5) + 1
             when 1 then 'one'
             when 2 then 'two'
             when 3 then 'three'
             when 4 then 'four'
             when 5 then 'five'
             else 'unknonw'
           end,
           'some_codice'
    from generate_series(1,200000) as g(id);