All Products
Search
Document Center

Hologres:GEN_RANDOM_UUID

Last Updated:Mar 26, 2026

GEN_RANDOM_UUID generates a version 4 universally unique identifier (UUID) composed of random hexadecimal integers. Use it to assign unique IDs to rows at write time—such as a DEFAULT value on a primary key column—without managing ID sequences manually.

Prerequisites

Before you begin, ensure that you have:

  • A Hologres database

  • Permission to run DDL statements in that database

Install the pgcrypto extension

GEN_RANDOM_UUID requires the pgcrypto extension. Install it once per database before calling the function.

-- Install the extension.
CREATE EXTENSION pgcrypto;

-- Remove the extension if no longer needed.
DROP EXTENSION pgcrypto;
The extension is scoped to the database. If you create a new database, run CREATE EXTENSION pgcrypto in that database as well.

Syntax

GEN_RANDOM_UUID() → uuid

Returns a uuid value. For example: 3a5401f1-0f0c-4380-8611-78e654efd86d

A UUID has a fixed length of 128 bits. Its value ranges from 00000000-0000-0000-0000-000000000000 to ffffffff-ffff-ffff-ffff-ffffffffffff. For more information about the UUID data type, see Data types.

Examples

Generate a UUID on demand

SELECT GEN_RANDOM_UUID();

Result:

gen_random_uuid
------------------------------------
3a5401f1-0f0c-4380-8611-78e654efd86d

Auto-populate a UUID column at insert time

Call gen_random_uuid() inside an INSERT ... SELECT statement to assign a unique ID to each row from a source table.

-- Create a source table and populate it with sample data.
CREATE TABLE t_source (a INT);
INSERT INTO t_source SELECT * FROM generate_series(1, 5);

-- Create a destination table with a UUID column.
CREATE TABLE t_result (a INT, b UUID);

-- Insert rows and generate a UUID for each one.
INSERT INTO t_result SELECT *, gen_random_uuid() FROM t_source;

-- Verify the results.
SELECT * FROM t_result;

Result:

 a |                  b
---+--------------------------------------
 1 | 27477537-abc4-4c17-9cf3-91c856a3b298
 2 | 2522b1ce-fdf3-4b14-a3c3-78f4baac5186
 3 | c69959a3-ad40-424f-9eb1-3271d0c6a8d4
 4 | 5493e087-b1b8-47e2-8117-adea27aaa676
 5 | f0e55a29-e72e-42e6-99aa-486db4f8b624
(5 rows)