PostgreSQL UUID Generator
Generate UUIDs for PostgreSQL. Supports v4 (random) and v7 (time-ordered). Copy as SQL, array, or plain text.
Client-side only — nothing leaves your browser
Click Generate to create UUIDs
UUID v4 vs v7: Which to Use?
UUID v4 is completely random, making it unpredictable but potentially causing index fragmentation. UUID v7 encodes the timestamp in the first bits, making it time-sortable and much better for database primary keys. Use v7 for better INSERT performance.
PostgreSQL UUID Functions
- gen_random_uuid() — Built-in v4 (PG 13+)
- uuid_generate_v4() — uuid-ossp extension
- uuid_generate_v7() — Native in PG 17+
- uuid_generate_v5() — Deterministic from string
Quick Reference
| Task | SQL |
|---|---|
| Enable uuid-ossp | CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; |
| Generate v4 UUID | SELECT gen_random_uuid(); |
| UUID primary key | id UUID PRIMARY KEY DEFAULT gen_random_uuid() |
| String to UUID | SELECT '550e8400-e29b-41d4-a716-446655440000'::uuid; |
| UUID to string | SELECT uuid_column::text; |
| Remove hyphens | SELECT REPLACE(gen_random_uuid()::text, '-', ''); |
Frequently Asked Questions
What is the UUID data type in PostgreSQL?
PostgreSQL has a native UUID data type that stores 128-bit universally unique identifiers in a compact 16-byte format. It's more space-efficient than storing UUIDs as TEXT (which uses 36 bytes). The type validates format on input, supports indexing, and works with comparison operators. Use it for any column that holds UUIDs: CREATE TABLE users (id UUID PRIMARY KEY, name TEXT);
UUID vs SERIAL/BIGSERIAL: which is better for primary keys?
UUID pros: globally unique, safe for distributed systems, no sequence coordination, harder to guess. Cons: 16 bytes vs 4/8 bytes, slower index operations (mitigated by v7). Use UUID for distributed systems, APIs, or when hiding record counts matters. Use SERIAL for simple apps where performance is critical.
Should I use UUID v4 or v7 for PostgreSQL primary keys?
UUID v7 is recommended for primary keys in PostgreSQL. Unlike v4 (random), v7 is time-ordered which means new UUIDs are always larger than old ones. This dramatically improves B-tree index performance, reduces page splits, and makes INSERT operations faster. Use v4 only when you need unpredictable IDs for security reasons.
What's the performance impact of UUID primary keys?
Random UUIDs (v4) can cause index fragmentation and slower INSERTs due to random placement in B-tree indexes. UUID v7 solves this by being time-ordered. Storage is 16 bytes vs 4 bytes for INT. For most applications, the difference is negligible. Benchmark your specific workload if concerned.
Why does uuid_generate_v4() not exist in my PostgreSQL?
The uuid_generate_v4() function requires the uuid-ossp extension. Run: CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; Alternatively, PostgreSQL 13+ has built-in gen_random_uuid() which generates v4 UUIDs without any extension. For v7, you'll need PostgreSQL 17+ or a custom function.
Need a PostgreSQL GUI Client?
1bench is a modern database client for PostgreSQL and 20+ other databases. Query, browse, and manage your data visually.
Learn More