O'Reilly logo

PostgreSQL 9 Administration Cookbook by Hannu Krosing, Simon Riggs

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Generating test data

DBAs frequently need to generate test data for a variety of reasons, whether it's to set up a test database or just to generate a test case for an SQL performance issue.

How to do it...

To create a table of test data we need the following:

  • Some rows
  • Some columns
  • Some order

Rows

To generate a lot of rows of data, we use something named a "set returning function". You can write your own, though PostgreSQL includes a couple of very useful ones.

You can generate a sequence of rows using a query, like the following:

postgres=# SELECT * FROM generate_series(1,5);
generate_series
-----------------
1
2
3
4
5
(5 rows)

or you can generate a list of dates, like the following:

postgres=# SELECT date(generate_series(now(), now() + '1 week', '1 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required