Skip to content

pg_uuidv7

Create UUIDv7 values in postgres

Overview

PackageVersionCategoryLicenseLanguage
pg_uuidv71.7.0FUNCMPL-2.0C
IDExtensionBinLibLoadCreateTrustRelocSchema
4520pg_uuidv7NoYesNoYesNoYes-

Version

TypeRepoVersionPG VerPackageDeps
EXTPGDG1.7.01817161514pg_uuidv7-
RPMPGDG1.7.01817161514pg_uuidv7_$v-
DEBPGDG1.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
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
d12.aarch64
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
d13.x86_64
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
d13.aarch64
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
u22.x86_64
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
u22.aarch64
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
u24.x86_64
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
u24.aarch64
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
u26.x86_64
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
u26.aarch64
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0
PGDG 1.7.0

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 repository is added and enabled:

pig repo add pgdg -u          # Add PGDG repo and update cache

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

Install
pig install pg_uuidv7;          # Install for current active PG version
pig
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
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
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
);

Was this page helpful?