SELECT string_agg(concat(
substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil(random() * 26)::integer, 1),
substring('abcdefghijklmnopqrstuvwxyz', ceil(random() * 26)::integer, 1),
substring('0123456789', ceil(random() * 10)::integer, 1),
substring('!#$%&()*+,-./:;<=>?@[]^', ceil(random() * 23)::integer, 1)
), '')
FROM generate_series(1,8);
SELECT string_agg(concat(substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil(random() * 26)::integer, 1),
substring('abcdefghijklmnopqrstuvwxyz', ceil(random() * 26)::integer, 1),
substring('0123456789', ceil(random() * 10)::integer, 1),
substring('!#$%&()*+,-./:;<=>?@[]^', ceil(random() * 23)::integer, 1)
),
'' ORDER BY RANDOM()
)
FROM generate_series(1,8);
SELECT string_agg(unnest, '') FROM(
select * FROM (select unnest(string_to_array(string_agg(concat(
substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil(random() * 26)::integer, 1),
substring('abcdefghijklmnopqrstuvwxyz', ceil(random() * 26)::integer, 1),
substring('0123456789', ceil(random() * 10)::integer, 1),
substring('!#$%&()*+,-./:;<=>?@[]^', ceil(random() * 23)::integer, 1)
), ''), NULL)) FROM generate_series(1,8)) x ORDER BY RANDOM()) as random_chars
select string_agg(substring, '') from (
SELECT
substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil(random() * 26)::integer, 1)
FROM generate_series(1,12) -- 12 capital letters
union
SELECT
substring('abcdefghijklmnopqrstuvwxyz', ceil(random() * 26)::integer, 1)
FROM generate_series(1,12) -- 12 low case letters
union
SELECT
substring('0123456789', ceil(random() * 10)::integer, 1)
FROM generate_series(1,6) -- 6 digits
union
SELECT
substring('!#$%&()*+,-./:;<=>?@[]^', ceil(random() * 23)::integer, 1)
FROM generate_series(1,2) -- 2 other chars
) seria;