Skip to content

Instantly share code, notes, and snippets.

@stereoscott
Created June 22, 2014 17:38
Show Gist options
  • 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/[email protected]
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)), '-', ''));
@ilyakaznacheev
Copy link

Wow, cool!

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