Created
June 22, 2014 17:38
-
-
Save stereoscott/ceb8e446ff1afed5968e to your computer and use it in GitHub Desktop.
PostgreSQL random text function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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)), '-', '')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great routine. The avoidance of similar looking digits was a nice touch.