首页 > 解决方案 > 将 100 万个随机数据插入 PostgreSQL

问题描述

表结构是这样的:

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

我想在该表中插入随机的 100 万条数据。有没有办法做到这一点?

标签: postgresql

解决方案


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.


推荐阅读