Supabase database

Why is auth.uid() null, and why does my Supabase RLS seem not to work?

auth.uid() reads the user id from the access token on the request, so it is null whenever no signed-in user’s token reaches the database: in the SQL editor, before sign-in finishes, or from a server client built without the user’s session. RLS “not working” is usually the same mix-up the other way round: the SQL editor and the service_role key skip RLS, so they see every row.

TL;DR

  • Supabase’s dashboard runs your SQL as postgres, which has bypassrls. In our lab it saw every row and auth.uid() was null.
  • Inside a transaction, set local role authenticated plus the user’s claims gave the owner’s id and only her rows.
  • A request with only the anon key ran as anon and got []; Supabase’s test function shows which role a request really has.
  • The service_role (secret) key returned every row. Never “fix” null with it in the browser.

Where auth.uid() gets its value

Every Data API request carries a token. Supabase Auth issues one to each signed-in user; the anon or publishable key stands in when nobody is signed in. The API switches to the role in the token (anon, authenticated or service_role) and passes the claims to Postgres. auth.uid() returns the sub claim, the user id. No user token, no sub, so null.

A policy like using ((select auth.uid()) = user_id) then compares null with every row’s owner, which is never true, so the query returns nothing.

Why RLS “doesn’t work” in the SQL editor

Supabase’s roles page says queries you run in the dashboard execute as postgres, and its RLS guide says postgres has bypassrls. So the editor shows every row whatever your policies say, and auth.uid() is null because there is no request token.

lab output, 5 Dec 2026 (Postgres 17.11 and 18.6, PostgREST 12.2.12)
-- as postgres, like the SQL editor
select auth.uid(), count(*) from notes:   NULL | 3

Your policies may be fine. You’re just looking at them as a role they don’t apply to.

Test as a real user in the SQL editor

Switch role inside a transaction and set the claims the API would set. Then roll back, so nothing sticks. This is the same approach as Supabase’s pgTAP examples.

test-as-user.sql
begin;
set local role authenticated;
set local request.jwt.claims = '{"sub":"11111111-1111-1111-1111-111111111111","role":"authenticated"}';
select auth.uid(), count(*) from public.notes;   -- only that user's rows
rollback;

begin;
set local role anon;
set local request.jwt.claims = '{"role":"anon"}';
select auth.uid(), count(*) from public.notes;   -- what a logged-out visitor sees
rollback;
lab output
as authenticated alice: 11111111-1111-1111-1111-111111111111 | 2
as anon:                NULL | 0

Use a real user id from Authentication → Users. For the full method, with a stranger and the owner, see RLS policies explained.

Why auth.uid() is null in the app

  • The query runs before sign-in finishes, for example on page load before the session is restored. It goes out with the anon key.
  • A server route or Edge Function creates its own client without passing the user’s token, so it runs as anon. Pass the request’s Authorization header through (see secure Edge Functions).
  • The client was created with the service role or secret key, so there is no user at all.

Supabase’s troubleshooting page suggests a test function. Call it from the exact place your query runs:

test-session.sql (from Supabase's troubleshooting page)
create function test_authorization_header() returns json
  language sql as $$ select auth.jwt(); $$;
-- const { data } = await supabase.rpc('test_authorization_header'); console.log(data)
drop function test_authorization_header;  -- when you're done
lab output
anon key only, rpc test_authorization_header: {"exp": 1796489573, "role": "anon"}       (no "sub": auth.uid() is null)
anon key only, GET /notes:                    [] [HTTP 200]
alice's access token, GET /notes:             [{"body":"alice: dentist 3pm"}, {"body":"new"}] [HTTP 200]
service_role key, GET /notes:                 every user's notes [HTTP 200]

The fix that turns a bug into a leak

The quickest way to make rows appear is to swap in the service_role key, or the new secret key. Supabase says it authorises access through a role with bypassrls and must never be used in the browser. In our lab it returned every user’s notes. If that key is in your frontend, anyone can read it from your JavaScript and do the same. Rotate it, then fix the session instead. Supabase also notes that a secret-key client that carries a user’s token runs under that user’s policies, which is a separate source of confusion on servers.

How to check it’s fixed

  1. The test function returns "role": "authenticated" and your user id as sub at the point of the query.
  2. Signed in, you see your rows. In a private window, signed out, you see none.
  3. A second account sees only its own rows (the two-account test).
  4. No secret or service_role key in your built JavaScript. Paste your bundle into the free key check below.

Ask your AI builder to fix it

Prompt for your AI builder
In my Supabase app auth.uid() is null when this query runs, so RLS returns no rows. Do not use the service_role or secret key in client code, do not disable RLS and do not add using (true) policies. Find why the request has no user session: check the query waits for the session (onAuthStateChange / getSession) before running, and that any server route or Edge Function creates its client with the user's Authorization header. Show me the code change before you apply it.

Questions people ask

Why does auth.uid() return null in the Supabase SQL editor?

The SQL editor runs as postgres and sends no user token, so there is no user id to read. Inside a transaction, set local role authenticated and set local request.jwt.claims with a user's sub to test as that user.

Why can I see all rows in the SQL editor even with RLS on?

Supabase runs dashboard queries as postgres, which has the bypassrls attribute, so policies don't apply. Your app runs as anon or authenticated, where they do.

Does the service role key bypass RLS?

Yes. It runs as service_role, which has bypassrls. In our lab it returned every user's rows. Keep it, and the new secret key, on the server only. Supabase notes it follows the user's policies if the request also carries a user's access token.

Why is auth.uid() null in my Edge Function?

The function's Supabase client isn't sending the caller's access token. Create the client with the request's Authorization header so queries run as that user, or check the caller yourself before using the service role.

Sources and lab

  • Supabase: Postgres roles, read 5 Dec 2026: dashboard queries execute as postgres; service_role bypasses RLS.
  • Supabase: Row Level Security, “Bypassing Row Level Security”: postgres has bypassrls; secret key and service_role; the user-token note; pgTAP examples using set local role.
  • Supabase: empty data array troubleshooting (updated 4 Nov 2026): the test_authorization_header function.
  • Lab: Postgres 17.11 and 18.6 with PostgREST 12.2.12 and a Supabase role shim (auth.uid() reads the token claims the way Supabase’s does), 5 Dec 2026. Our postgres is a superuser; Supabase’s has bypassrls; both skip RLS. Synthetic data only. Same results on both versions.

The checks behind this guide, each with example code, a self-check and the fix.

Free check

Are any of these keys also in your code?

Paste code, a .env or your built JavaScript bundle into the free key check. It runs in your browser: no request is sent and nothing is stored. It flags secret keys (AI providers, Stripe, AWS, GitHub, database URLs), tells a Supabase anon key from a service role key, and links to where to rotate each one.

Open the free key check

Free, no sign-upNothing leaves your browserSee a sample result

Written by Manraj Singh Dhillon. Last checked 5 Dec 2026. Code samples are synthetic and run against your own project; nothing here names a real app. Spotted a mistake? Tell us.