Skip to main content
0 reactions

How to Generate a UUID in PostgreSQL

Featured card for How to Generate a UUID in PostgreSQL, with a terminal window listing gen_random_uuid(), uuidv7(), DEFAULT uuidv7() and a UUID starting 01a21b9e-db78.
How to generate a UUID in PostgreSQL.

A UUID is an ID that no one else will ever make, written as 32 hexadecimal digits in groups, such as 01a21b9e-db78-748e-96e5-0a4bd6b7a6e9. Here’s how to generate a UUID in PostgreSQL with its two built-in functions, and how to use one as a table’s ID.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxSQL
gen_random_uuid()
uuidv7()
  • gen_random_uuid(): a random UUID, version 4. uuidv4() is another name for it.
  • uuidv7(): a UUID that starts with the time it was made, version 7, so newer UUIDs sort after older ones.
  • Both return a value of type uuid, which stores the 128-bit ID in 16 bytes.

See UUID Functions (opens in a new tab) and the UUID Type (opens in a new tab) in the PostgreSQL documentation.

The Example

A smart-home app gives every device an ID that stays unique, even across different databases.

Step 1: Connect

Connect to your server with psql as the postgres user, as Part 2 of the PostgreSQL Tutorial shows. psql connects you to the postgres database, and its prompt is postgres=#.

Step 2: Generate a Random UUID

postgres=#SQL
SELECT gen_random_uuid();
OutputPlain text
           gen_random_uuid
--------------------------------------
 df8e3baa-80a9-4b6c-87e5-d8d0e18dbc1b
(1 row)

Every call returns a new random value.

Step 3: Generate a Time-Ordered UUID

postgres=#SQL
SELECT uuidv7();
OutputPlain text
                uuidv7
--------------------------------------
 01a21b9e-db78-748e-96e5-0a4bd6b7a6e9
(1 row)
postgres=#SQL
SELECT uuidv7();
OutputPlain text
                uuidv7
--------------------------------------
 01a21b9e-db78-78ce-9bb6-5c18e582aa97
(1 row)

Both start with 01a21b9e-db78: the first 12 digits are the time in milliseconds, and these two were made in the same millisecond. The rest is random, so they still differ.

Step 4: Use a UUID as a Table’s ID

Create a database for the example and connect to it:

postgres=#SQL
CREATE DATABASE gadgets;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c gadgets
OutputPlain text
You are now connected to database "gadgets" as user "postgres".

DEFAULT uuidv7() tells PostgreSQL to fill in a new UUID whenever a row arrives without one:

gadgets=#SQL
CREATE TABLE devices (id uuid DEFAULT uuidv7(), name text);
OutputPlain text
CREATE TABLE
gadgets=#SQL
INSERT INTO devices (name) VALUES ('Kitchen speaker');
OutputPlain text
INSERT 0 1
gadgets=#SQL
INSERT INTO devices (name) VALUES ('Desk lamp');
OutputPlain text
INSERT 0 1
gadgets=#SQL
SELECT * FROM devices;
OutputPlain text
                  id                  |      name
--------------------------------------+-----------------
 01a21b9e-dbeb-7018-9d39-fa168cda1962 | Kitchen speaker
 01a21b9e-dbeb-76dd-a939-88031302586c | Desk lamp
(2 rows)

Step 5: See When an ID Was Made

uuid_extract_timestamp() reads the time back out of a version 7 UUID:

gadgets=#SQL
SELECT name, uuid_extract_timestamp(id) FROM devices;
OutputPlain text
      name       |    uuid_extract_timestamp
-----------------+-------------------------------
 Kitchen speaker | 2026-11-27 11:38:45.547+05:30
 Desk lamp       | 2026-11-27 11:38:45.547+05:30
(2 rows)

Which One to Use

  • uuidv7() for a table’s IDs: new rows’ IDs sort in the order they were made.
  • gen_random_uuid() when the ID mustn’t reveal anything, because anyone can read the time out of a version 7 UUID, as Step 5 showed.

Clean Up

To remove the example, connect to another database and drop the test database:

gadgets=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE gadgets;
OutputPlain text
DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo this instead
Storing UUIDs in a text columnAny text is accepted, even a value that isn’t a UUID, and each one takes more than twice the space.Use the uuid type.
Using uuidv7() where the creation time must stay privateThe time can be read back, as uuid_extract_timestamp() showed.Use gen_random_uuid().

FAQ

Do I need an extension?

No. Both functions are built into PostgreSQL 19. The uuid-ossp extension is only for other kinds of UUID.

Is a UUID better than a number that counts up?

It’s unique everywhere, so it suits data made in several places. A number is shorter and easier to read; Identity vs Serial in PostgreSQL: Which to Use for IDs shows that option.

Conclusion

gen_random_uuid() makes a random UUID and uuidv7() a time-ordered one. Give a uuid column DEFAULT uuidv7(), and every new row gets its ID by itself.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments