postgres random data generation

vim data/words.list

add words from linux.words.backup.fedora.standard

Then, you can easily create a ton of no sense description BUT searchable using dictionary words with the following steps:

1) Create table and function. genArray gets all the elements in an array and the number of times it needs to concatenate.

CREATE TABLE TblRndm(id serial PRIMARY KEY, descr text);

CREATE OR REPLACE FUNCTION genArray(word text[], count int) RETURNS text AS $$
  SELECT string_agg(word[random()*(array_length(word,1)-1)+1], ' ') FROM generate_series(1,count) g(i)
$$
VOLATILE
LANGUAGE SQL;

Once you have all in place, run the insert using CTE:

WITH BS(syl) AS(
  SELECT (string_to_array(pg_read_file('words.list')::text,E'\n')) 
) 
INSERT INTO TblRndm(descr)
SELECT genArray(BS.syl, 3) FROM BS, generate_series(1,10000);

And now, select as usual:

postgres=# select * from TblRndm limit 10;
 id |             descr
----+-------------------------------
  1 | abasing abhorrer abdomens
  2 | Aaron aback abed
  3 | abducted abets abasing
  4 | abets abases aback
  5 | abets abandoned abbreviations
  6 | abetted abbey abhorrent
  7 | abashing abetting abashes
  8 | abase abbot abdomen
  9 | abhorred abdomen abbreviating
 10 | abbey abasements Aaron
(10 rows)

Leave a Reply

Your email address will not be published. Required fields are marked *