pg_uuidv7

Create UUIDv7 values in postgres

Overview

PackageVersionCategoryLicenseLanguage
pg_uuidv71.7.0FUNCMPL-2.0C
IDExtensionBinLibLoadCreateTrustRelocSchema
4540pg_uuidv7NoYesNoYesNoYes-
Relatedpg_idkit pgx_ulid uuid-ossp sequential_uuids pg_hashids permuteseq

Version

TypeRepoVersionPG VerPackageDeps
EXTMIXED1.7.01817161514pg_uuidv7-
RPMPGDG1.7.01817161514pg_uuidv7_$v-
DEBPIGSTY1.7.01817161514postgresql-$v-pg-uuidv7-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
u22.x86_64
u22.aarch64
u24.x86_64
u24.aarch64

Build

You can build the DEB packages for pg_uuidv7 using pig build:

pig build pkg pg_uuidv7         # build DEB packages

Install

You can install pg_uuidv7 directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

pig repo add pgsql -u          # Add repo and update cache

Install the extension using pig or apt/yum/dnf:

pig install pg_uuidv7;          # Install for current active PG version
pig ext install -y pg_uuidv7 -v 18  # PG 18
pig ext install -y pg_uuidv7 -v 17  # PG 17
pig ext install -y pg_uuidv7 -v 16  # PG 16
pig ext install -y pg_uuidv7 -v 15  # PG 15
pig ext install -y pg_uuidv7 -v 14  # PG 14
dnf install -y pg_uuidv7_18       # PG 18
dnf install -y pg_uuidv7_17       # PG 17
dnf install -y pg_uuidv7_16       # PG 16
dnf install -y pg_uuidv7_15       # PG 15
dnf install -y pg_uuidv7_14       # PG 14
apt install -y postgresql-18-pg-uuidv7   # PG 18
apt install -y postgresql-17-pg-uuidv7   # PG 17
apt install -y postgresql-16-pg-uuidv7   # PG 16
apt install -y postgresql-15-pg-uuidv7   # PG 15
apt install -y postgresql-14-pg-uuidv7   # PG 14

Create Extension:

CREATE EXTENSION pg_uuidv7;

Usage

pg_uuidv7: Create valid version 7 UUIDs in PostgreSQL

CREATE EXTENSION pg_uuidv7;

Functions

FunctionDescription
uuid_generate_v7()Generate a new UUIDv7
uuid_v7_to_timestamptz(uuid)Extract the timestamp from a UUIDv7
uuid_timestamptz_to_v7(timestamptz [, bool])Convert a timestamp to a UUIDv7 (set second arg to true to zero the random bits)

Examples

-- Generate a UUIDv7
SELECT uuid_generate_v7();
-- 018570bb-4a7d-7c7e-8df4-6d47afd8c8fc

-- Extract timestamp from UUIDv7
SELECT uuid_v7_to_timestamptz('018570bb-4a7d-7c7e-8df4-6d47afd8c8fc');
-- 2023-01-02 04:26:40.637+00

-- Convert timestamp to UUIDv7
SELECT uuid_timestamptz_to_v7('2023-01-02 04:26:40.637+00');
-- 018570bb-4a7d-7630-a5c4-89b795024c5d

-- For date range queries, zero the random bits
SELECT uuid_timestamptz_to_v7('2023-01-02 04:26:40.637+00', true);
-- 018570bb-4a7d-7000-8000-000000000000

-- Use as primary key
CREATE TABLE events (
  id uuid NOT NULL DEFAULT uuid_generate_v7() PRIMARY KEY,
  data text
);

Last Modified 2026-03-12: add pg extension catalog (95749bf)