Timescale Logo

PostgreSQL Extensions: Intro to uuid-ossp

The uuid-ossp extension is a powerful tool for developers working with PostgreSQL. This extension generates universally unique identifiers (UUIDs) in accordance with the Open Software Foundation's (OSF) Distributed Computing Environment (DCE) standard.

UUIDs are 128-bit values that are globally unique. No matter where you generate a UUID, it will always be different from any UUID generated anywhere else at any time.

Installing the uuid-ossp Extension

To install the uuid-ossp extension, you need to have access to your PostgreSQL server and the necessary privileges to execute the CREATE EXTENSION command. Follow the steps below:

1. Log into your PostgreSQL server.

2. Load it into your shared_preloaded_libraries in postgresql.conf, as it will require additional shared memory. If you’re using Timescale, you can simply skip this step, as the extension is already preloaded.

3. Connect to the database where you want to install the extension.

4. Run the following SQL command:

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

This command will install the uuid-ossp extension if it is not already installed. On Timescale, you can find available extensions by going to Operations > Extensions from your service overview, which will also give you installation instructions.

Using the uuid-ossp Extension

Once the uuid-ossp extension is installed, you can use it to generate UUIDs. The extension provides several functions for generating UUIDs, including:

- uuid_generate_v1(): This function generates a UUID using a combination of the current time and the MAC address of the computer executing the function.

- uuid_generate_v4(): This function generates a random UUID.

Here is an example of how to use these functions:

INSERT INTO my_table (id, name) VALUES (uuid_generate_v4(), 'My Name');

In this example, a new UUID is generated for each row inserted into the table. Note that PostgreSQL supports a UUID data type for your table.

Time-Series Use Cases for the uuid-ossp Extension

The uuid-ossp extension is particularly useful in time-series data scenarios. For instance, in IoT applications where data is collected from multiple devices at different times, each data point can be assigned a unique UUID. This ensures that each data point can be uniquely identified, regardless of when or where it was collected.

Using uuid-ossp extension with Timescale and time-series data

If you're using Timescale, a high-performance time-series PostgreSQL++ database, the uuid-ossp extension can be particularly useful. You can use the uuid-ossp extension to generate a unique identifier for each time-series data point you insert into your Timescale database.

Here's an example:

INSERT INTO conditions (id, time, location, temperature, humidity) VALUES (uuid_generate_v4(), NOW(), 'office', 70.0, 50.0);

In this example, a new UUID is generated for each row inserted into the conditions table. This ensures that each data point can be uniquely identified, even if the time and location are the same for multiple data points.

Did you know Timescale can dramatically improve the performance for many UUID use cases? Check out our getting started guide! 

Timescale Logo

Subscribe to the Timescale Newsletter

By submitting, I acknowledge Timescale’s Privacy Policy
2024 © Timescale Inc. All rights reserved.