Skip to content

Instantly share code, notes, and snippets.

@stereoscott
Created June 22, 2014 17:38
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stereoscott/ceb8e446ff1afed5968e to your computer and use it in GitHub Desktop.
Save stereoscott/ceb8e446ff1afed5968e to your computer and use it in GitHub Desktop.
PostgreSQL random text function
-- http://www.postgresql.org/message-id/bddc86150908200534p14723ac2vd1ef223af095d822@mail.gmail.com
CREATE OR REPLACE FUNCTION
random_text(INTEGER)
RETURNS TEXT
LANGUAGE SQL
AS $$
SELECT array_to_string(array(
SELECT SUBSTRING('23456789abcdefghjkmnpqrstuvwxyz'
FROM floor(random()*31)::int+1 FOR 1)
FROM generate_series(1, $1)), '');
$$;
-- use UUID, only returns hex
SELECT lower(replace(cast(uuid_generate_v4() as varchar(50)), '-', ''));
@masilver99
Copy link

Great routine. The avoidance of similar looking digits was a nice touch.

@ilyakaznacheev
Copy link

Wow, cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment