MoneerOmar
MoneerOmar

Reputation: 225

How to automatically populate database tables

I have some tables in Postgres that I want to populate with dummy data. Is there a tool/utility that can help with this, that?

  1. respects constraints
  2. respects foreign keys
  3. given schema definition (selected tables), automatically populate with data

Upvotes: 0

Views: 1686

Answers (2)

Adriano_Pinaffo
Adriano_Pinaffo

Reputation: 1699

Not exactly what you mentioned but here you can find a python program that (creates and/or) auto-populate tables with fake data. For anyone who is interested, check it out here if you want.

enter image description here

Upvotes: 1

sdev95
sdev95

Reputation: 142

You could try to read up generate_series() in postgresql, this function could be used to provide a table with dummy data.

insert into testscheme.testtable (id, name) 
    select generate_series, 'name1' from generate_series(1,250)

This will provide 250 rows in testtable. You can build further on this. For example replace 1,250 with the primary key contraint. For example with

nextval('testtable_pk'::regclass)

And if you would like the foreign key respected try to use a subselect: postgresql order by random(), select rows in random order:

select MAX(names) from testscheme.names ORDER BY random()

If you wrap this all in a function you can make a script to fill stuff :) Hope I could help.

Here is the documentation of generate_series() https://www.postgresql.org/docs/12/functions-srf.html

Upvotes: 1

Related Questions