Created
February 23, 2024 05:56
-
-
Save fdelbos/2e80a838c83a80c6df7c9341a9b883e8 to your computer and use it in GitHub Desktop.
A function to generate a random URL safe string with PostgreSQL
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
create or replace function random_string(nb_bytes integer) | |
returns text | |
as $$ | |
select | |
trim('=' | |
from | |
( | |
select | |
replace ( ( | |
select | |
replace ( ( | |
select | |
encode(gen_random_bytes(nb_bytes)::bytea, 'base64')), '/', '_')), '+', '-') | |
) | |
); | |
$$ language sql; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment