Skip to content

Instantly share code, notes, and snippets.

@fabiolimace
Last active December 29, 2020 23:40
Show Gist options
  • Save fabiolimace/f9a55bd29e4e054fc4de27410ae620c3 to your computer and use it in GitHub Desktop.
Save fabiolimace/f9a55bd29e4e054fc4de27410ae620c3 to your computer and use it in GitHub Desktop.
Select statement for generating random UUIDs (v4) on PostgreSQL
/**
* Generate a random-based UUID (v4)
*
* RFC-4122 compliant.
*
* Tags: uuid guid uuid-generator guid-generator generator random rfc4122 rfc-4122
*/
select (overlay(overlay(md5(random()::text || random()::text || current_timestamp::text) placing '4' from 13) placing ( to_hex(8 + (extract(microseconds from statement_timestamp())::integer % 4)) ) from 17))::uuid;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment