DEV Community

Dmitry Romanoff
Dmitry Romanoff

Posted on

Populating a table in PostgreSQL with one million records of random data.

The given table is

create table my_table_1(a bigint, b varchar(1000), c timestamp, d bool, e float);
Enter fullscreen mode Exit fullscreen mode

Let’s populate this table in PostgreSQL with one million records of random data.

This query inserts 1,000,000 rows of randomly generated data into the table my_table_1

INSERT INTO my_table_1 (a, b, c, d, e)
SELECT
 (random() * 10000)::bigint, -- generates a random integer between 0 and 10,000, which is cast to a bigint 
 ARRAY_TO_STRING(ARRAY(SELECT chr((65 + (random() * 25))::int) FROM generate_series(1, (1000*random())::int)), ''), -- generates a random string of characters of variable length, where each character is an uppercase letter (A-Z), and concatenates them into a single string without any delimiter.
 NOW() - random() * INTERVAL '365 days', -- generates a random date within the past year 
 random() < 0.5, -- generates a random boolean value (true or false) 
 random() * 100 -- generates a random floating-point number between 0 and 100
FROM generate_series(1, 1000000);
Enter fullscreen mode Exit fullscreen mode

Check how many records are in the my_table_1 table.

select count(1) from my_table_1;
Enter fullscreen mode Exit fullscreen mode

Output:
1000000

Check the distribution of records in the my_table_1 table by the column d:

select d, count(1) from my_table_1 group by 1;
Enter fullscreen mode Exit fullscreen mode

Output:
false 500605
true 499395

ask_dima@yahoo.com

Top comments (0)