Search code examples
postgresqlinsertauto-increment

Fastest way to create a million records in Postgresql?


what is the fastest way to create million records, where the first column is a incremental number (not a primary key) and the other fields are with their default values ?

Do I have to create a PG procedure to be faster, because it is internal i.e. does not go trough DB driver ??!!


Solution

  • You can use generate_series and omit all columns that should use the default:

    insert into test(id) select generate_series(1,1000000);

    To insert the same sequence into multiple columns, you can reuse the series:

    insert into test(id,id2) 
    select seq,seq 
    from generate_series(100,110) v(seq);