PostgreSQL - UUID Data Type
Last Updated :
13 Nov, 2024
The UUID (Universally Unique Identifier) is a 128-bit identifier defined by RFC 4122. It generates globally unique values using algorithms that ensure no duplication, making it ideal for distributed systems. PostgreSQL supports UUID as a data type and provides extensions for UUID generation, which is particularly useful in multi-database applications or distributed systems where unique identifiers are crucial.
In this article, we will explain the PostgreSQL UUID Data Type along with its syntax, examples, and usage scenarios. This guide will help us understand how to effectively implement and manage UUIDs in our PostgreSQL databases.
Why Use UUIDs in PostgreSQL?
UUIDs offer a key advantage over the SERIAL data type by ensuring uniqueness not only within a single database but across multiple databases or systems. This makes UUIDs an optimal choice for applications that require global uniqueness. UUIDs are commonly used in distributed systems and microservices architectures, where identifiers must be unique even across network boundaries.
Installing the UUID Extension in PostgreSQL
While PostgreSQL allows storing and comparing UUID values, it does not include built-in functions for generating them. Instead, it relies on third-party modules, such as the 'uuid
-
ossp'
module, which implements standard algorithms for UUID generation.
To install the uuid-ossp
extension, execute the following command:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
Generating UUIDs in PostgreSQL
The uuid-ossp
module provides several functions to generate UUIDs based on different algorithms. These functions make it easy to create UUIDs directly within PostgreSQL, supporting various use cases and levels of randomness for unique key generation.
1. UUID Version 1
UUID version 1 combines the computer’s MAC address, the current timestamp, and a random value to produce a unique identifier. Use the 'uuid_generate_v1()'
function as shown below:
Query:
SELECT uuid_generate_v1();
Output

2. UUID Version 4
UUID version 4 generates a UUID based entirely on random numbers, ensuring a high degree of uniqueness. Use the 'uuid_generate_v4()'
function as follows:
Query:
SELECT uuid_generate_v4();
Output

Example of PostgreSQL UUID Data Type
Let us take a look at an example of UUID Data Type in PostgreSQL to better understand the concept. In this example we will make a table whose primary key is a UUID data type. In supplement, the values of the primary key column will be produced automatically through the 'uuid_generate_v4()'
function.
Query:
CREATE TABLE contacts (
contact_id uuid DEFAULT uuid_generate_v4 (),
first_name VARCHAR NOT NULL,
last_name VARCHAR NOT NULL,
email VARCHAR NOT NULL,
phone VARCHAR,
PRIMARY KEY (contact_id)
);
INSERT INTO contacts (first_name, last_name, email, phone)
VALUES
('Raju', 'Kumar', '[email protected]', '408-237-2345'),
('Nikhil', 'Aggarwal', '[email protected]', '408-237-2344'),
('Anshul', 'Aggarwal', '[email protected]', '408-237-2343'
);
SELECT * FROM contacts;
Output

Important Points About PostgreSQL UUID Data Type
- UUID versions: PostgreSQL supports multiple versions of UUIDs (v1, v4, etc.), each using different algorithms for generation.
- Extensions for UUIDs: PostgreSQL’s core does not natively generate UUIDs; it relies on extensions like '
uuid
-
ossp'
or 'pgcrypto'
.
- UUID format: UUIDs are represented in a standard textual format (8-4-4-4-12 hexadecimal digits) which can be easily read and parsed by humans and systems.
- Client-side vs. server-side generation: UUIDs can be generated on the client-side (in application code) or server-side (in the database). Client-side generation can reduce the load on the database.
Conclusion
PostgreSQL's support for the UUID data type provides a reliable method for unique data identification across distributed systems and databases. By utilizing the uuid-ossp extension, PostgreSQL allows the generation of UUIDs with standard functions, enhancing the flexibility and security of our database designs. Using UUIDs also helps prevent conflicts across databases, making them ideal for scalable and distributed environments.
Similar Reads
PostgreSQL - Data Types PostgreSQL is a powerful, open-source relational database management system that supports a wide variety of data types. These data types are essential for defining the nature of the data stored in a database column. which allows developers to define, store, and manipulate data in a way that aligns w
5 min read
PostgreSQL - CHAR Data Type The CHAR data type in PostgreSQL is one of the essential character data types for storing fixed-length strings. Unlike VARCHAR, which stores variable-length data, CHAR is used when we need to store a fixed-length string.This article will explain the CHAR data type in PostgreSQL, its syntax, common u
5 min read
PostgreSQL - TEXT Data Type PostgreSQL provides a highly flexible character data type known as TEXT, designed to store character strings of virtually unlimited length. Unlike the VARCHAR data type, which can be limited to a specified length, the TEXT data type offers the same efficiency and performance without the length const
3 min read
PostgreSQL - TIME Data Type In PostgreSQL, the TIME data type is essential for applications that require precise time tracking, such as scheduling systems and event logging. This data type allows for accurate time-based entries without storing date information. PostgreSQLâs TIME data type also supports fractional seconds for u
4 min read
PostgreSQL - JSON Data Type JSON (JavaScript Object Notation) is a widely used format for storing data in the form of key-value pairs. Its popularity comes from being easy for humans to read and understand, making it ideal for communication between servers and clients. This readability and ease of use have made JSON a standard
4 min read