PostgreSQL has the uuid-ossp
extension which ships with the standard distributions and it has 5 standard algorithms for generating uuid
s. Note that a guid
is the Microsoft version of a uuid
, conceptually they are the same thing.
CREATE EXTENSION "uuid-ossp";
Then:
SELECT uuid_generate_v4();
Note also that, once you installed the extension, PostgreSQL has an actual binary uuid
type, with a length of 16 bytes. Working with the binary type is much faster than working with the text equivalent and it takes up less space. If you do need the string version, you can simply cast it to text
:
SELECT uuid_generate_v4()::text;
PostgreSQL 13+
You can now use the built-in function gen_random_uuid()
to get a version 4 random UUID.