Neon is a serverless PostgreSQL platform with instant branching, autoscaling, and a scale-to-zero compute model. Because each Neon branch is an independent PostgreSQL database, UUID primary keys are the natural choice for IDs — auto-increment integers would conflict across branches. UUID v7 improves on UUID v4 by adding monotonic ordering that keeps B-tree indexes efficient across the serverless write pattern. See UUID v7 Performance Benchmarks for the index fragmentation numbers.
Enable pg_uuidv7 on Neon
-- Run in any Neon branch console or via migration
CREATE EXTENSION IF NOT EXISTS pg_uuidv7;
-- Verify
SELECT uuidv7(); -- 018fbe3a-4c5d-7b12-8f3e-1a2b3c4d5e6f
The extension is available on all Neon projects on PostgreSQL 16 and 17. No plan upgrade required.
Schema: UUID v7 default primary key
CREATE TABLE orders (
id uuid PRIMARY KEY DEFAULT uuidv7(),
customer text NOT NULL,
total numeric NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
-- UUID v7 embeds the timestamp — created_at is redundant for ordering
-- but useful for human-readable display
CREATE INDEX idx_orders_created ON orders (id); -- B-tree on UUID v7 = time order
Because UUID v7 encodes creation time, ORDER BY id and ORDER BY created_at produce the same result. The single B-tree index on id handles both range queries and pagination. See UUID v7 for Cursor Pagination.
Neon branching workflow with UUID v7
# Create a feature branch
neon branches create --name feature/new-orders
# Branch inherits all UUID v7 values from main
# New rows in the branch get new UUID v7 values (later timestamps)
# When branch is merged/promoted, IDs never collide with main
-- In the feature branch: insert test data
INSERT INTO orders (customer, total) VALUES ('test-user', 99.99);
-- id = 018fbe3a-... (timestamp = branch creation time + offset)
-- Back on main: production data
-- id = 018fbe39-... (earlier timestamp)
-- No collision — UUID v7 monotonicity across branches
Prisma + Neon + UUID v7
// schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL") // Neon connection string (pooled)
}
model Order {
id String @id @default(dbgenerated("uuidv7()")) @db.Uuid
customer String
total Decimal
createdAt DateTime @default(now())
}
-- Migration: 0001_enable_uuidv7.sql
CREATE EXTENSION IF NOT EXISTS pg_uuidv7;
See UUID in Prisma for the full migration and typed ID pattern.
Drizzle + Neon + UUID v7
// schema.ts
import { pgTable, uuid, text, numeric } from 'drizzle-orm/pg-core';
import { sql } from 'drizzle-orm';
export const orders = pgTable('orders', {
id: uuid('id').primaryKey().default(sql`uuidv7()`),
customer: text('customer').notNull(),
total: numeric('total').notNull(),
});
// db.ts — Neon serverless driver
import { neon } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/neon-http';
import * as schema from './schema';
const sql = neon(process.env.DATABASE_URL!);
export const db = drizzle(sql, { schema });
See UUID in Drizzle ORM for the migration setup.
Neon HTTP API with UUID v7
Neon’s serverless driver makes SQL over HTTP easy — UUID v7 cursor pagination works naturally:
import { neon } from '@neondatabase/serverless';
const sql = neon(process.env.DATABASE_URL!);
// Cursor pagination — UUID v7 as cursor
async function getOrders(cursor?: string, limit = 20) {
if (cursor) {
return sql`
SELECT id, customer, total
FROM orders
WHERE id > ${cursor}
ORDER BY id
LIMIT ${limit}
`;
}
return sql`
SELECT id, customer, total
FROM orders
ORDER BY id
LIMIT ${limit}
`;
}
UUID v7 cursors are stable across Neon scale-to-zero restarts because the cursor is a value in the database, not a connection-level sequence. See UUID v7 for Cursor Pagination.
Further reading
- PostgreSQL 18 Native UUID v7
- UUID in Prisma — v7 Setup and Migration
- UUID in Drizzle ORM — v7 Setup
- UUID v7 for Cursor Pagination
- UUID in Databases — Storage and Indexing
External references
Frequently asked questions
Does Neon support UUID v7 natively?
Neon runs PostgreSQL 17, which supports the pg_uuidv7 extension via CREATE EXTENSION IF NOT EXISTS pg_uuidv7. PostgreSQL 18's built-in uuidv7() will be available once Neon upgrades to PG 18. Until then, pg_uuidv7 is the recommended path. See PostgreSQL 18 Native UUID v7.
How do I use UUID v7 with Neon and Prisma?
In your Prisma schema, set default(dbgenerated("uuidv7()")) on your id field and enable the pg_uuidv7 extension via a migration. Neon's connection pooler (PgBouncer) is fully compatible with Prisma's @prisma/client. See UUID in Prisma.
Can I use UUID v7 across Neon database branches?
Yes. Neon branches are point-in-time copies of the database — UUID v7 values generated in a branch are monotonically ordered within that branch. Since UUID v7 embeds a Unix millisecond timestamp, values from different branches will sort correctly by creation time when merged or compared, as long as the generating application clocks are synchronised (standard NTP).