What is uuid in postgresql
Last updated: April 1, 2026
Key Facts
- PostgreSQL's UUID data type stores 128-bit universally unique identifiers in a standard format
- The uuid-ossp extension provides functions like uuid_generate_v4() to automatically generate UUIDs
- UUID values are larger than traditional integer primary keys but ensure uniqueness across distributed systems
- PostgreSQL can use UUID as primary key with auto-generation using DEFAULT uuid_generate_v4()
- UUIDs are non-sequential and provide better security for exposed identifiers compared to auto-incrementing integers
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.
More What Is in Daily Life
Also in Daily Life
More "What Is" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- PostgreSQL Documentation - uuid-ossp ExtensionPostgreSQL License
- Wikipedia - Universally Unique IdentifierCC-BY-SA-4.0