What is uuid in postgresql

Last updated: April 1, 2026

Quick Answer: UUID is a universally unique identifier data type in PostgreSQL that stores 128-bit values to uniquely identify records across databases. PostgreSQL supports UUID through the uuid data type, which can be generated using the uuid_generate_v4() function from the uuid-ossp extension.

Key Facts

Understanding UUID in PostgreSQL

UUID (Universally Unique Identifier) is a data type in PostgreSQL that stores 128-bit values designed to be globally unique across systems. Unlike auto-incrementing integers, UUIDs can be generated independently on multiple systems without coordination, making them ideal for distributed databases and microservices architectures.

Enabling UUID Support

PostgreSQL doesn't include UUID generation functions by default. To use UUIDs, you must first enable the uuid-ossp extension by running CREATE EXTENSION "uuid-ossp"; This extension provides several UUID generation functions including uuid_generate_v4(), which creates random UUIDs based on random numbers.

Creating UUID Columns

You can define a UUID column in PostgreSQL using the uuid data type. A common pattern is to set a UUID column as the primary key with automatic generation: CREATE TABLE users (id uuid PRIMARY KEY DEFAULT uuid_generate_v4(), name TEXT); This ensures every new record receives a unique UUID automatically without application intervention.

Advantages and Considerations

UUIDs provide significant advantages for distributed systems, databases that merge data from multiple sources, and applications requiring privacy-focused identifiers. However, they consume 16 bytes per value compared to 4-8 bytes for integers, slightly increasing storage and indexing overhead. UUIDs are non-sequential, which can impact B-tree index efficiency compared to sequential identifiers.

UUID Versions

PostgreSQL supports multiple UUID versions. UUID v4 (random) is most commonly used for general purposes. UUID v1 is based on timestamp and MAC address. The uuid-ossp extension provides functions for generating different versions, allowing you to choose the most appropriate identifier strategy for your application's needs.

Related Questions

How do I generate UUIDs automatically in PostgreSQL?

Enable the uuid-ossp extension and set a column's DEFAULT to uuid_generate_v4(). Every new row will automatically receive a unique UUID without application code.

Should I use UUID or INTEGER for primary keys?

Use UUID for distributed systems, microservices, or when privacy is important. Use INTEGER for simpler single-database applications where sequential IDs are preferred.

What is the difference between UUID versions?

UUID v1 is based on timestamp and hardware MAC address, while v4 uses random generation. UUID v4 is generally preferred for security and simplicity in modern applications.

Sources

  1. PostgreSQL Documentation - uuid-ossp ExtensionPostgreSQL License
  2. Wikipedia - Universally Unique IdentifierCC-BY-SA-4.0