DEV Community

Dmitry Romanoff
Dmitry Romanoff

Posted on

2

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

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay