Skip to main content

How to Create UUID in PostgreSQL

Complete guide with gen_random_uuid() and uuid-ossp

How to Create UUID in PostgreSQL. Call gen_random_uuid() to generate a PostgreSQL UUID instantly. A short guide to the rust uuid crate covers features, parsing a string back into a typed value and version choices.

Generate UUID for PostgreSQL

SELECT gen_random_uuid();
550e8400-e29b-41d4-a716-446655440000

PostgreSQL has had built-in UUID generation since version 13 — no extension required. Call gen_random_uuid() directly in a query or as a column default to get a random, RFC 4122-compliant identifier stored natively as 16 bytes instead of a 36-character string. Below you'll find copy-paste ready SQL for generating a PostgreSQL UUID, casting a string back into the UUID type, and the extension-based alternatives for versions before 13.

How to Generate a UUID in PostgreSQL

The simplest way to generate a UUID in PostgreSQL (13 and later) is the built-in gen_random_uuid() function — no extensions, no setup. Developers who want an unpredictable identifier with no embedded metadata can use the uuid v4 generator to get one instantly.

psql
-- PostgreSQL 13+: gen_random_uuid() is built in, no extension needed
CREATE TABLE users (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    email VARCHAR(255) NOT NULL
);

-- Generate one directly in a query
SELECT gen_random_uuid();
-- Output: 550e8400-e29b-41d4-a716-446655440000

Explanation

Convert a String to a UUID in PostgreSQL

A UUID sent as a string — from an API request or an application layer — needs to be cast to PostgreSQL's native UUID type before it can be compared or stored efficiently. Pick version 1, 4 or 7 from the drop-down and the uuid generator returns a fresh identifier on each click.

psql
-- Cast a string literal to the UUID type
SELECT '550e8400-e29b-41d4-a716-446655440000'::uuid;

-- Or using CAST()
SELECT CAST('550e8400-e29b-41d4-a716-446655440000' AS uuid);

-- Comparing a string against a UUID column works via implicit cast
SELECT * FROM users WHERE id = '550e8400-e29b-41d4-a716-446655440000';

Explanation

Other Options for Generating UUIDs in PostgreSQL

PostgreSQL doesn't have native UUID v7 support yet, and versions before 13 need the uuid-ossp extension for any built-in generation function at all. Under heavy inserts, a sql server uuid reference also explains a sequential option that keeps a clustered index from fragmenting.

psql
-- PostgreSQL < 13: enable the uuid-ossp extension once per database
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- Version 4 (random) via uuid-ossp
SELECT uuid_generate_v4();

-- Version 1 (timestamp + MAC address) via uuid-ossp
SELECT uuid_generate_v1();

-- UUID v7 (time-sortable) isn't built in yet -- generate it in application
-- code (see the python uuid / rust uuid guides) and insert it as a plain UUID.

Explanation

About the author

Vinitha Pu

Founder and designer

Vinitha Pu is a graphic designer based in Palakkad, Kerala, who builds free, privacy-friendly browser tools. She studied Textile and Apparel Design at Avinashilingam University and completed a C-Tech in Graphic Design at Image Creative Education in 2025. She designs, builds and maintains the UUID generators, validator, decoder and the language and database UUID guides on UUID Generator.

  • Content author: Vinitha Pu
  • Fact checked by: Vinitha Pu. Every generator is checked against the RFC 4122 and RFC 9562 layouts and every code example is run before it goes live.
  • Published Updated

Vinitha Pu on Behance · Vinitha Pu on LinkedIn

Comments & Feedback

Share your experience or ask questions about this tool

Copied!