PostgreSQL Tutorial: UUID Data Type

August 2, 2023

Summary: in this tutorial, you will learn about the PostgreSQL UUID data type and how to generate UUID values using a supplied module.

Table of Contents

Introduction to PostgreSQL UUID type

UUID stands for Universal Unique Identifier defined by RFC 4122 and other related standards. A UUID value is 128-bit quantity generated by an algorithm that make it unique in the known universe using the same algorithm. The following shows some examples of the UUID values:


As you can see, a UUID is a sequence of 32 digits of hexadecimal digits represented in groups separated by hyphens.

Because of its uniqueness feature, you often found UUID in the distributed systems because it guarantees a better uniqueness than the SERIAL data type which generates only unique values within a single database.

To stores UUID values in the PostgreSQL database, you use the UUID data type.

Generating UUID values

PostgreSQL allows you store and compare UUID values but it does not include functions for generating the UUID values in its core.

Instead, it relies on the third-party modules that provide specific algorithms to generate UUIDs. For example the uuid-ossp module provides some handy functions that implement standard algorithms for generating UUIDs.

To install the uuid-ossp module, you use the CREATE EXTENSION statement as follows:


The IF NOT EXISTS clause allows you to avoid re-installing the module.

To generate the UUID values based on the combination of computer’s MAC address, current timestamp, and a random value, you use the uuid_generate_v1() function:

SELECT uuid_generate_v1();

The function generated the following a UUID value:

(1 row)

If you want to generate a UUID value solely based on random numbers, you can use the uuid_generate_v4() function. For example:

SELECT uuid_generate_v4();
(1 row)

For more information on the functions for UUID generation, check it out the uuid-ossp module documentation.

Creating a table with UUID column

We will create a table whose primary key is UUID data type. In addition, the values of the primary key column will be generated automatically using the uuid_generate_v4() function.

First, create the contacts table using the following statement:

CREATE TABLE contacts (
    contact_id uuid DEFAULT uuid_generate_v4 (),
    first_name VARCHAR NOT NULL,
    last_name VARCHAR NOT NULL,
    phone VARCHAR,
    PRIMARY KEY (contact_id)

In this statement, the data type of the contact_id column is UUID. The contact_id column has a default values provided by the uuid_generate_v4() function, therefore, whenever you insert new row without specifying the value for the contact_id column, PostgreSQL will call the uuid_generate_v4() function to generate the value for it.

Second, insert some data into the contacts table:

INSERT INTO contacts (

Third, query all rows in the contacts table using the following SELECT statement:


PostgreSQL UUID example

As you can see the contact_id column has been populated by the UUID values generated by the uuid_generate_v4() function.

In this tutorial, you have learned how to use PostgreSQL UUID data type and how to generate UUID values using the uuid-ossp module.

See more

PostgreSQL Tutorial: Data Types

PostgreSQL Documentation: UUID Type