Created
June 15, 2022 20:30
-
-
Save daspn-dock/9cceb69d0addeb7b6ffd1ebcdd995d20 to your computer and use it in GitHub Desktop.
PostgreSQL function to generate an unique ID of type ULID
This file contains hidden or 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 EXTENSION IF NOT EXISTS pgcrypto; | |
| CREATE OR REPLACE FUNCTION generate_ulid() RETURNS uuid | |
| AS $$ | |
| SELECT (lpad(to_hex(floor(extract(epoch FROM clock_timestamp()) * 1000)::bigint), 12, '0') || encode(gen_random_bytes(10), 'hex'))::uuid; | |
| $$ LANGUAGE SQL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.