Skip to content

Instantly share code, notes, and snippets.

@fdelbos
Created February 23, 2024 05:56
Show Gist options
  • Save fdelbos/2e80a838c83a80c6df7c9341a9b883e8 to your computer and use it in GitHub Desktop.
Save fdelbos/2e80a838c83a80c6df7c9341a9b883e8 to your computer and use it in GitHub Desktop.
A function to generate a random URL safe string with PostgreSQL
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