Supabase is an open-source Firebase alternative built on PostgreSQL. By default, Supabase tables use gen_random_uuid() (UUID v4) for primary keys. Switching to UUID v7 improves B-tree index performance for high-insert workloads because UUID v7 is monotonically increasing — it prevents the page fragmentation caused by random UUID v4 inserts. See UUID v4 vs v7 for the performance trade-offs.
Enable pg_uuidv7 in Supabase
Via the Supabase dashboard: Database → Extensions → search “uuid” → enable pg_uuidv7.
Via migration:
-- supabase/migrations/0001_enable_uuidv7.sql
CREATE EXTENSION IF NOT EXISTS pg_uuidv7;
-- Verify
SELECT uuidv7();
-- 018fbe3a-4c5d-7b12-8f3e-1a2b3c4d5e6f
Table with UUID v7 default primary key
CREATE TABLE posts (
id uuid PRIMARY KEY DEFAULT uuidv7(),
user_id uuid NOT NULL REFERENCES auth.users(id),
title text NOT NULL,
body text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
-- RLS
ALTER TABLE posts ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users see their own posts"
ON posts FOR SELECT
USING (user_id = auth.uid());
CREATE POLICY "Users insert their own posts"
ON posts FOR INSERT
WITH CHECK (user_id = auth.uid());
UUID v7 id sorts chronologically — ORDER BY id is equivalent to ORDER BY created_at for the primary key. The user_id column keeps the UUID v4 from Supabase Auth. See UUID in Databases for the storage pattern.
Supabase client with UUID v7 (JavaScript)
import { createClient } from '@supabase/supabase-js';
import { v7 as uuidv7 } from 'uuid';
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
);
// Option 1: let the database generate UUID v7 (DEFAULT uuidv7())
const { data, error } = await supabase
.from('posts')
.insert({ user_id: userId, title: 'Hello', body: 'World' })
.select('id')
.single();
// Option 2: generate UUID v7 in the client for idempotency
const postId = uuidv7();
const { data: post } = await supabase
.from('posts')
.upsert({ id: postId, user_id: userId, title: 'Hello', body: 'World' })
.select()
.single();
Client-side UUID v7 generation is useful for optimistic UI updates — the ID is known before the insert completes. See UUID as Idempotency Keys.
Cursor pagination with Supabase and UUID v7
// Keyset pagination — UUID v7 as cursor
async function getPosts(cursor?: string, limit = 20) {
let query = supabase
.from('posts')
.select('id, title, body')
.order('id', { ascending: true })
.limit(limit);
if (cursor) {
query = query.gt('id', cursor);
}
const { data, error } = await query;
const nextCursor = data && data.length === limit ? data[data.length - 1].id : null;
return { data, nextCursor };
}
UUID v7 cursors are stable, opaque, and encode creation time. See UUID v7 for Cursor Pagination.
Prisma + Supabase + UUID v7
// schema.prisma
model Post {
id String @id @default(dbgenerated("uuidv7()")) @db.Uuid
userId String @db.Uuid
title String
body String
createdAt DateTime @default(now())
}
-- Migration: enable extension before Prisma uses it
-- prisma/migrations/0001_enable_uuidv7/migration.sql
CREATE EXTENSION IF NOT EXISTS pg_uuidv7;
See UUID in Prisma for the full typed ID setup.
Further reading
- UUID v4 vs v7 — performance comparison
- PostgreSQL 18 Native UUID v7
- UUID in Prisma — v7 Setup and Migration
- UUID v7 for Cursor Pagination
- UUID as Idempotency Keys
External references
Frequently asked questions
Does Supabase support UUID v7?
Supabase runs PostgreSQL 15 and 16, which do not have a built-in uuidv7() function. Enable the pg_uuidv7 extension via the Supabase dashboard (Database → Extensions) or in a migration with CREATE EXTENSION IF NOT EXISTS pg_uuidv7. Once enabled, uuidv7() is available as a column default. See PostgreSQL 18 Native UUID v7 for when it becomes a built-in.
Can I mix Supabase Auth UIDs (UUID v4) with UUID v7 row IDs?
Yes. Supabase Auth generates UUID v4 for user IDs (auth.uid()). Your application tables can use UUID v7 primary keys — just store the user's UUID v4 as a foreign key column (user_id uuid REFERENCES auth.users(id)). Row Level Security policies reference auth.uid() regardless of the primary key format on your table.
How do I use UUID v7 with Supabase Row Level Security?
Row Level Security policies use auth.uid() to match the current user's UUID v4. Your row primary key (id) can be UUID v7 — RLS only cares about the user_id column that references auth.users. Example: USING (user_id = auth.uid()) works regardless of whether id is UUID v4 or UUID v7.