uuid-ossp: Unique Identifiers
uuid-ossp extension can be used to generate 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".
Enable the extension#
- Go to the Database page in the Dashboard.
- Click on Extensions in the sidebar.
- Search for "uuid-ossp" and enable the extension.
uuid-ossp extension is enabled by default and cannot be disabled.
Once the extension is enabled, you now have access to a
Creates a UUID value based on the combination of computer’s MAC address, current timestamp, and a random value.
Creates UUID values based solely on random numbers. You can also use Postgres's built-in
gen_random_uuid() function to generate a UUIDv4.
Within a query#
As a Primary Key#
Automatically create a unique, random ID in a table:
1create table contacts ( 2 id uuid default uuid_generate_v4(), 3 first_name text, 4 last_name text, 5 6 primary key (id) 7);