Files
supabase/apps/temp-docs/docs/guides/database/extensions/uuid-ossp.mdx
2022-04-26 14:17:19 +02:00

99 lines
2.0 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
id: uuid-ossp
title: "uuid-ossp: Unique Identifiers"
description: A UUID generator for PostgreSQL.
---
# uuid-ossp: Unique Identifiers
import Tabs from '@theme/Tabs';
import TabsPanel from '@theme/TabsPanel';
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
### Enabling
<Tabs
defaultActiveId="UI"
values={[
{label: 'UI', value: 'UI'},
{label: 'SQL', value: 'SQL'},
]}>
<TabsPanel id="UI" label="UI">
```sh
1. Go to the Database page.
2. Click on "Extensions" in the sidebar.
3. Search for "uuid-ossp".
4. Click the toggle.
```
</TabsPanel>
<TabsPanel id="SQL" label="SQL">
```sql
-- Example: enable the "uuid-ossp" extension
create extension "uuid-ossp" with schema extensions;
-- Example: disable the "uuid-ossp" extension
drop extension if exists "uuid-ossp";
```
Even though the SQL code is `create extension`, this is the equivalent of "enabling the extension".
To disable an extension you can call `drop extension`.
It's good practice to create the extension within a separate schema (like `extensions`) to keep your database clean.
</TabsPanel>
</Tabs>
### 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 computers MAC address, current timestamp, and a random value.
### `uuid_generate_v4()`
Creates UUID values based solely on random numbers.
## Examples
### Within a query
```sql
select uuid_generate_v4();
```
### As a Primary Key
Automatically create a unique, random ID in a table:
```sql
create table contacts (
id uuid default uuid_generate_v4(),
first_name text,
last_name text,
primary key (id)
);
```
## Resources
- [The Basics Of PostgreSQL `UUID` Data Type](https://www.postgresqltutorial.com/postgresql-uuid/).