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
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
SELECT gen_random_uuid();
gen_random_uuid
--------------------------------------
df8e3baa-80a9-4b6c-87e5-d8d0e18dbc1b
(1 row)
Every call returns a new random value.
Step 3: Generate a Time-Ordered UUID
SELECT uuidv7();
uuidv7
--------------------------------------
01a21b9e-db78-748e-96e5-0a4bd6b7a6e9
(1 row)
SELECT uuidv7();
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:
CREATE DATABASE gadgets;
CREATE DATABASE
\c gadgets
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:
CREATE TABLE devices (id uuid DEFAULT uuidv7(), name text);
CREATE TABLE
INSERT INTO devices (name) VALUES ('Kitchen speaker');
INSERT 0 1
INSERT INTO devices (name) VALUES ('Desk lamp');
INSERT 0 1
SELECT * FROM devices;
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:
SELECT name, uuid_extract_timestamp(id) FROM devices;
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:
\c postgres
You are now connected to database "postgres" as user "postgres".
DROP DATABASE gadgets;
DROP DATABASE
Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Storing UUIDs in a text column | Any 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 private | The 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
- PostgreSQL 19 documentation: UUID Functions (opens in a new tab)
- PostgreSQL 19 documentation: UUID Type (opens in a new tab)
Related
- PostgreSQL Tutorial — Part 5: Data Types Explained
- Identity vs Serial in PostgreSQL: Which to Use for IDs
- How to Check a Column’s Data Type in PostgreSQL
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