How to Generate a UUID in PostgreSQL

    Generate v4 on PostgreSQL 13+ or v7 on PostgreSQL 18+, and use a native UUID column.

    Need a UUID right now? Generate one instantly in your browser.

    Reviewed 2026-09-11. Check runtime requirements before running an example.

    PostgreSQL has a native uuid data type and, since version 13, a built-in gen_random_uuid() function that returns a random version 4 UUID. No extension is required on modern Postgres.

    The examples show generating a UUID directly and using one as an auto-populated primary key.

    Generate a random UUID

    Call gen_random_uuid() in any query. It returns a value of the native uuid type.

    sql
    SELECT gen_random_uuid();
    -- 9b2e4f1a-3c6d-4e58-9f0a-1b2c3d4e5f60

    Time-ordered UUID v7 in PostgreSQL 18+

    PostgreSQL 18 adds built-in uuidv7() generation and uuid_extract_timestamp(). These functions are version-specific; gen_random_uuid() remains available for random v4 identifiers.

    sql
    SELECT uuidv7();
    SELECT uuid_extract_timestamp(uuidv7());

    UUID primary key with a default

    Set the column type to uuid and default it to gen_random_uuid() so every inserted row gets an identifier automatically.

    sql
    CREATE TABLE users (
        id    uuid PRIMARY KEY DEFAULT gen_random_uuid(),
        email text NOT NULL
    );
    
    INSERT INTO users (email) VALUES ('a@example.com')
    RETURNING id;

    Older Postgres and the uuid-ossp extension

    On Postgres versions before 13, enable the uuid-ossp extension and use uuid_generate_v4(). gen_random_uuid() is preferred on current versions.

    sql
    CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
    SELECT uuid_generate_v4();

    Default values and existing records

    A default generates a value when the column is omitted or DEFAULT is used. An explicit NULL does not invoke the default. Changing a default does not rewrite existing IDs or foreign keys. Test migrations separately from this example.

    The statements below target PostgreSQL 18+. They demonstrate syntax, not measured performance. Use RETURNING to retrieve the ID generated for the inserted row.

    sql
    CREATE TEMP TABLE uuid_example (
        id uuid PRIMARY KEY DEFAULT uuidv7(),
        label text NOT NULL
    );
    INSERT INTO uuid_example (label) VALUES ('example') RETURNING id;
    SELECT id, uuid_extract_version(id), uuid_extract_timestamp(id)
    FROM uuid_example;

    Frequently asked questions

    Do I need an extension to generate UUIDs in PostgreSQL?
    Not on PostgreSQL 13+. gen_random_uuid() is built in. Older versions need the uuid-ossp extension and uuid_generate_v4().
    How do I make a UUID primary key auto-generate in Postgres?
    Declare the column as uuid PRIMARY KEY DEFAULT gen_random_uuid(); each insert then gets a UUID automatically.
    Should I store UUIDs as text in Postgres?
    No. Use the native uuid type. It stores the value in 16 bytes and is more efficient than text.

    Official references