// guide · Supabase · Drizzle · Prisma · RLS · Dec 3, 2026
Using Drizzle or Prisma with Supabase? Your RLS policies don't protect those queries
Short answer
- Drizzle and Prisma connect with
DATABASE_URL. On Supabase that is usually thepostgresrole, which owns your tables and has BYPASSRLS. Theprismauser from Supabase's Prisma guide is created withbypassrlstoo. - In that session
auth.uid()is null and none of your policies run. Each query's WHERE clause is the only thing keeping one user out of another user's rows. - Fix: filter every user-facing query by the user id from the server-verified session, or run user queries in an RLS transaction (
set local role authenticatedplus the user's JWT claims). - Test it: as user B, try to read and update user A's row through your API.
Why this keeps happening
AI assistants love a Supabase + Drizzle (or Prisma) stack: Supabase for auth, an ORM for type-safe queries. The migrations get careful auth.uid() policies, and then the server code runs db.select().from(notes) and trusts RLS to filter it. On Dec 3, 2026 builders on X were swapping agent rules for exactly this trap (“Drizzle connects as the Postgres owner role, which bypasses Row Level Security”). Nothing looks broken in testing, because your own account sees your own data either way.
// policy in supabase/migrations: users only see their own notes
// create policy "own notes" on notes for all to authenticated
// using ((select auth.uid()) = user_id);
// app/api/notes/route.ts (Drizzle over DATABASE_URL)
export async function GET() {
const rows = await db.select().from(notes); // "RLS will filter it"... it won't
return Response.json(rows); // every user's notes
}Why the policies are skipped
PostgreSQL's row security docs: superusers and roles with the BYPASSRLS attribute always bypass row security, and table owners normally do too, unless the table uses FORCE ROW LEVEL SECURITY. Supabase's RLS docs note that on Supabase the owner is postgres, “which hasbypassrls”, and the Supabase Drizzle quickstart connects with the pooler connection string for that user. Supabase's Prisma quickstart creates a dedicated user with create user "prisma" ... bypassrls createdb. And because no user JWT is attached to that connection, auth.uid() returns null.
We reproduced it on PostgreSQL 17 with a notes table holding two users' rows and an auth.uid() = user_id policy: the owner role saw both rows, a BYPASSRLS role saw both rows, and the same query inside an RLS transaction as user A returned only A's row. After FORCE ROW LEVEL SECURITY the owner saw 0 rows, but the BYPASSRLS role still saw both, which is why FORCE alone doesn't help a postgres connection on Supabase.
Check which role your app connects as
-- Run with the SAME connection string your app uses (psql "$DATABASE_URL")
select current_user, rolsuper, rolbypassrls
from pg_roles where rolname = current_user;
-- Who owns the tables, and is RLS on / forced?
select c.relname, pg_get_userbyid(c.relowner) as owner,
c.relrowsecurity as rls_on, c.relforcerowsecurity as rls_forced
from pg_class c join pg_namespace n on n.oid = c.relnamespace
where n.nspname = 'public' and c.relkind = 'r' order by 1;
-- rolbypassrls = true, or current_user = owner (without forced RLS):
-- your policies do not apply to anything this connection runs.Queries made through the Supabase client (supabase.from("notes")) are different: they go through the Data API with the user's access token and run as anon or authenticated, so your policies apply. The bypass only affects the ORM path.
Fix 1: filter every query by the verified user
// Fix 1: every user-facing query filters by the VERIFIED user
import { and, eq } from "drizzle-orm";
export async function GET() {
const supabase = await createClient(); // @supabase/ssr server client
const { data: { user } } = await supabase.auth.getUser(); // verified by Supabase Auth
if (!user) return new Response("Unauthorized", { status: 401 });
const rows = await db.select().from(notes).where(eq(notes.userId, user.id));
return Response.json(rows);
}
// Updates and deletes need the ownership check too, not just the id:
await db.update(notes).set({ body })
.where(and(eq(notes.id, id), eq(notes.userId, user.id)));Take the user id from a session your server verified (supabase.auth.getUser() or getClaims()), never from the request body, a URL parameter or a header the browser sets. Updates and deletes need the ownership condition too: where id = $1 alone lets any signed-in user change anyone's row.
Fix 2: run user queries under RLS
// Fix 2: run user queries under RLS (pattern from the Drizzle RLS docs)
import { sql } from "drizzle-orm";
export async function asUser<T>(claims: { sub: string; role?: string }, fn: (tx: typeof db) => Promise<T>) {
return db.transaction(async (tx) => {
// claims must come from a token your server verified, never from the request body
await tx.execute(sql`select set_config('request.jwt.claims', ${JSON.stringify(claims)}, true)`);
await tx.execute(sql`set local role authenticated`);
return fn(tx as unknown as typeof db);
});
}
// auth.uid() now returns claims.sub and your policies filter the rows:
const rows = await asUser(claims, (tx) => tx.select().from(notes));This is the pattern the Drizzle RLS docs show for Supabase: inside a transaction, set the JWT claims and switch to the authenticated role, so auth.uid() works and Postgres enforces your policies. set local and set_config(..., true) only last until the transaction ends, so they don't leak to the next request on a pooled connection. Keep a separate plain connection for migrations, cron jobs and webhooks that really need full access.
The two-user test
-- Two-user test in the SQL editor (rolls back, changes nothing)
begin;
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"<user A uuid>","role":"authenticated"}', true);
select count(*) from notes; -- only A's rows
select set_config('request.jwt.claims', '{"sub":"<user B uuid>","role":"authenticated"}', true);
select count(*) from notes; -- only B's rows
rollback;
-- Then do the same through your app: sign in as B and call the API with A's note id.Prompt to paste into Cursor, Claude Code or Lovable
My app uses Supabase RLS policies with auth.uid(), but my server queries Postgres with Drizzle/Prisma over DATABASE_URL, which connects as an RLS-bypassing role (postgres or a bypassrls prisma user). So the policies don't protect these queries.
1. List every Drizzle/Prisma query in server code on a table that has an auth.uid() policy. For each, say whether it filters by the signed-in user's id and where that id comes from.
2. Flag queries with no WHERE, updates/deletes by id without an ownership check, and any user id taken from the request body, URL or a client-sent header.
3. Get the user from the server-verified session (supabase.auth.getUser() or getClaims()) and add the ownership filter to each flagged query, OR wrap user requests in a transaction that runs set local role authenticated and set_config('request.jwt.claims', <verified claims>, true).
4. Keep the owner connection only for migrations, cron jobs and webhooks.
Show the diff before changing anything, then add a test where user B tries to read and update user A's row.Check it automatically
RescueMyRepo's free scan reads your migrations and server code without running anything. Since Dec 3, 2026 it flags Drizzle and Prisma queries on tables whose RLS policies use auth.uid(), when they run over a direct Postgres connection with no RLS transaction. A query with no WHERE on one of those tables is high, and filtered queries are medium, because the policies still aren't doing anything for them. Tests, scripts, seeds, migrations and admin, cron and webhook routes don't count as unfiltered.
FAQ
Does Drizzle or Prisma respect Supabase Row Level Security?
Not by default. RLS is enforced by Postgres for the role that runs the query. Drizzle and Prisma connect with DATABASE_URL, which on Supabase normally logs in as postgres. Supabase's RLS docs say postgres has bypassrls, and Postgres skips RLS for table owners and for roles with BYPASSRLS. auth.uid() is also null in that session, because no user JWT is attached. Your policies only run for queries that come through the Supabase client (the Data API) with the user's token, or for ORM queries you wrap in an RLS transaction.
What about the prisma user from Supabase's Prisma guide?
Supabase's Prisma quickstart creates it with create user "prisma" with password '...' bypassrls createdb, so it skips RLS too. That's fine for migrations and admin work, but it means every user-facing Prisma query must filter by the signed-in user itself.
Will ALTER TABLE ... FORCE ROW LEVEL SECURITY fix it?
Only for a table owner that does not have BYPASSRLS. Postgres applies forced RLS to the owner, but superusers and BYPASSRLS roles still skip it. On Supabase postgres has bypassrls, so FORCE alone doesn't change what a postgres connection sees. We checked this on PostgreSQL 17: after FORCE the owner role saw 0 rows, while a BYPASSRLS role still saw all of them.
Is this a vulnerability if my queries already filter by userId?
Not by itself. It means your RLS policies are a second lock that is never used on this path, so one forgotten where(eq(table.userId, user.id)), an update by id without an ownership check, or a userId read from the request body exposes other users' rows. RescueMyRepo reports it as medium when every query it found has a WHERE, and high when one reads or changes a policy-protected table with no WHERE at all.
How do I make Drizzle queries run under RLS?
Run each user request in a transaction that switches to the authenticated role and sets the verified JWT claims, as the Drizzle RLS docs show: set local role authenticated and select set_config('request.jwt.claims', <claims json>, true), then run your queries on that transaction. auth.uid() then returns the user's id and the policies apply. Keep the plain owner connection for migrations, cron jobs and webhooks.
Does RescueMyRepo check this?
Yes (added Dec 3, 2026). The free scan looks for RLS policies that use auth.uid() or auth.jwt() (SQL migrations or Drizzle pgPolicy), then for Drizzle or Prisma queries on those same tables in server code that runs over a direct Postgres connection with no RLS transaction. It lists the queries and marks the ones with no WHERE. Tests, scripts, seeds and migrations are skipped, and apps that already set request.jwt.claims or set local role are not flagged.
Related
- RLS is on and your Supabase data still leaks: 5 policy mistakes to check
- Is it safe to commit your Supabase anon key? (and the key you must never push)
- Supabase SECURITY DEFINER functions anyone can call
- Supabase views bypass RLS by default
Sources
- Supabase docs: Row Level Security
- Supabase docs: Postgres roles
- Supabase docs: Drizzle quickstart
- Supabase docs: Prisma quickstart
- Drizzle ORM docs: Row-Level Security
- PostgreSQL docs: Row Security Policies
Facts per the linked docs, read Dec 3, 2026. Bypass behavior reproduced on PostgreSQL 17.