Skip to main content

uuid-ossp: Unique Identifiers

The uuid-ossp extension can be used to generate a UUID.

Overview

A UUID is a "Universally Unique Identifer" and it is, for practical purposes, unique. This makes them particularly well suited as Primary Keys. It is occasionally referred to as a GUID, which stands for "Globally Unique Identifer".

Usage

Enable the extension

  1. Go to the Database page in the Dashboard.
  2. Click on Extensions in the sidebar.
  3. Search for "uuid-ossp" and enable the extension.

Note: Currently uuid-ossp extension is enabled by default and cannot be disabled.

The uuid type

Once the extension is enabled, you now have access to a uuid type.

uuid_generate_v1()

Creates a UUID value based on the combination of computer’s MAC address, current timestamp, and a random value.

uuid_generate_v4()

Creates UUID values based solely on random numbers.

Examples

Within a query

select uuid_generate_v4();

As a Primary Key

Automatically create a unique, random ID in a table:

create table contacts (
id uuid default uuid_generate_v4(),
first_name text,
last_name text,

primary key (id)
);

Resources