I'm trying to generate all possible strings of length x
using a fixed set of characters in PostgreSQL. For the simple case of x = 2
I can use the query below, but I cannot figure out how to do it for an arbitrary length (I'm assuming this will involve recursion):
with characters (c) as (
select unnest(array['a', 'b', 'c', 'd'])
)
select concat(c1.c, c2.c)
from characters c1
cross join characters c2
This generates aa
, ab
, ac
, ad
, ba
, bb
, bc
, bd
, etc.