Skip to content

Instantly share code, notes, and snippets.

View cbaatz's full-sized avatar

Carl Baatz cbaatz

View GitHub Profile
@cbaatz
cbaatz / gen_prefixed_id.sql
Created March 17, 2024 20:48
PostgreSQL function to generate a random ID using [0-9A-Za-z] with a fixed prefix.
-- SELECT gen_prefixed_id('account_', 20);
-- account_9f2oOuK23Xpv3rdtHnQY
-- Example usage in a table column definition:
-- id text DEFAULT gen_prefixed_id('account_', 16) PRIMARY KEY
-- Load the built-in pgcrypto module if not loaded.
CREATE EXTENSION IF NOT EXISTS pgcrypto;
-- Create a function that takes a text prefix and the length of the