uuid-ossp: Unique Identifiers
A guide from Supabase
Apps on Plentiful run on Supabase's open-source database, sign-in, file storage and live updates, so most of this guide applies. The differences: your app reaches its database through its own server — not Supabase's data API (supabase.from(…)) — there are no Edge Functions (your app's server does that work), and scheduled work uses Jobs. Where it says "Supabase dashboard", use your app's Dashboard. Ask the builder when in doubt.
The uuid-ossp extension can be used to generate a UUID.
Overview#
A UUID is a "Universally Unique Identifier" 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 Identifier".
Enable the extension#
Note:
Currently uuid-ossp extension is enabled by default and cannot be disabled.
- Go to the Database page in the Dashboard.
- Click on Extensions in the sidebar.
- Search for
uuid-osspand enable the extension.
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.
UUIDv1 leaks identifiable details, which might make it unsuitable for certain security-sensitive applications.
uuid_generate_v4()#
Creates UUID values based solely on random numbers. You can also use Postgres's built-in gen_random_uuid() function to generate a UUIDv4.
Examples#
Within a query#
1select uuid_generate_v4();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 primary key (id)6);