Database Reference
In-Depth Information
Either of those functions can be used to generate both rows and reasonable Primary Key
values for them.
Columns
Now, we want to generate a value for each column in the test table. We can break that down
into a series of functions, using the following examples as a guide:
Random integer
(random()*(2*10^9))::integer
Random bigint
(random()*(9*10^18))::bigint
Random numeric data
(random()*100.)::numeric(4,2);
Random length string, up to a maximum length
repeat('1',(random()*40)::integer)
Random length substring
substr('abcdefghijklmnopqrstuvwxyz',1, (random()*26)::integer)
Random string from a list of strings
(ARRAY['one','two','three'])[1+random()*3]
ORDER
We can put both together to generate our table:
postgres=# SELECT generate_series(1,10) as key
,(random()*100.)::numeric(4,2)
,repeat('1',(random()*25)::integer);
key | numeric | repeat
-----+---------+------------------------
1 | 83.05 | 1111
2 | 5.28 | 11111111111111
3 | 41.85 | 1111111111111111111111
4 | 41.70 | 11111111111111111
5 | 53.31 | 1
6 | 10.09 | 1111111111111111
7 | 68.08 | 111
8 | 19.42 | 1111111111111111
9 | 87.03 | 11111111111111111111
10 | 70.64 | 111111111111111
(10 rows)
 
Search WWH ::




Custom Search