Postgres gen_random_uuid() in Postgres 9, 10, 11, 12

Elvis Ciotti
Nov 4, 2021

--

The Postgres function gen_random_uuid() to create random UUID was only added in Postgres v13

If you need it for older versions, you can use this instead:

uuid_in(overlay(overlay(md5(random()::text || ':' || clock_timestamp()::text) placing '4' from 13) placing to_hex(floor(random()*(11-8+1) + 8)::int)::text from 17)::cstring)

If you need extra “salt”, add more content inside the ‘:’

--

--