Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inserting 1 million random data into PostgreSQL [duplicate]

Tags:

postgresql

the table structure is like this:

milliontable(
   name varchar(10),
   age integer,
   joindate date
)

and I want to insert random 1 million data into that table. Is there any way to do that?

like image 636
UFO_Rider Avatar asked Jun 15 '26 16:06

UFO_Rider


1 Answers

Use the random() function to generate random values:

INSERT INTO milliontable (name, age, joindate)
SELECT substr(md5(random()::text), 1, 10),
       (random() * 70 + 10)::integer,
       DATE '2018-01-01' + (random() * 700)::integer
FROM generate_series(1, 1000000);

It is usually a silly idea to store the age of a person in a table, as this number becomes wrong automatically as time goes by. Use the birthday.

like image 121
Laurenz Albe Avatar answered Jun 17 '26 04:06

Laurenz Albe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!