Generating Millions of Records Efficiently with PostgreSQL’s
Introduction:
PostgreSQL is a strong relational database management system that has a number of features that make data generation and manipulation chores more efficient and simple. In this blog post, we'll look at how to utilise the generate_series function to create millions of entries and the random() function to populate a hypothetical “payments” table with random amounts.

Background:
PostgreSQL’s generate_series function is a powerful tool for producing a series of values. It is widely used to generate numerical, date, or timestamp sequences. In this case, we’ll utilise it to generate a string of integers representing the number of records we wish to generate.
Example Scenario:
Let’s consider a scenario where we have a table named “payments” with a single column named “amount.” Our goal is to generate one million records with random amounts.
Creating the Payments Table:
CREATE TABLE payments ( id serial primary key, amount numeric );
Using generate_series
and random()
:
Now, let’s use the generate_series
function along with the random()
function to populate the "amount" column with random values.
INSERT INTO payments (amount)
SELECT floor(random() * 1000) + 1
FROM generate_series(1, 1000000);
Explanation:
generate_series(1, 1000000)
generates a series of numbers from 1 to 1,000,000.floor(random() * 1000) + 1
generates a random amount between 1 and 1000 for each row in the series.
By combining these functions in a single query, we efficiently generate one million records with random amounts in the “payments” table.
Conclusion:
When combined with other methods like random(), PostgreSQL’s generate_series function provides a powerful mechanism for effectively producing big datasets. Understanding and exploiting these PostgreSQL features can considerably simplify your data generation operations, whether you’re replicating data for testing or populating a database for analysis.