What auth.uid() actually reads
auth.uid() does not look at a login session in the database. It reads the sub claim of the JWT that the Supabase API placed into the transaction settings (request.jwt.claims) for the current request. You can see the exact definition with:
select pg_get_functiondef('auth.uid'::regproc);No JWT on the request means no sub, which means null. And null = user_id is never true, so policies built on it match nothing.
Where it is null
- The SQL Editor, psql and migrations. They connect straight to Postgres as
postgres, with no API request and no JWT. That role also owns the tables, so RLS doesn't filter it: queries return every row, which hides policy bugs. - pg_cron jobs and database webhooks, for the same reason.
- Server code that forgot the user's token. A Supabase client created with only the publishable/anon key in an API route runs as
anon. - Requests with the secret / service_role key. They bypass RLS, but there is no user, so
auth.uid()is null. A column defined asdefault auth.uid()gets null, and anot nullcolumn then fails withnull value in column "user_id" of relation "notes" violates not-null constraint.
Test a policy as a real user in SQL
Switch to the authenticated role and set the claims yourself, inside a transaction that you roll back:
begin;
set local role authenticated;
select set_config('request.jwt.claims',
json_build_object('sub', '<a-real-user-uuid>', 'role', 'authenticated')::text,
true);
select auth.uid(); -- now returns that uuid
select * from public.notes; -- only the rows this user may see
rollback;The third argument true keeps the setting local to the transaction. The SQL Editor also has a role selector that can run a query as a chosen user, which does the same thing for you.
Pass the user's token from server code
In an Edge Function or API route, forward the caller's Authorization header so the database sees the same user:
import { createClient } from 'npm:@supabase/supabase-js@2';
Deno.serve(async (req) => {
const supabase = createClient(
Deno.env.get('SUPABASE_URL')!,
Deno.env.get('SUPABASE_ANON_KEY')!,
{ global: { headers: { Authorization: req.headers.get('Authorization') ?? '' } } },
);
const { data, error } = await supabase.from('notes').select('*'); // runs as the caller, under RLS
return Response.json({ data, error });
});In Next.js, use the cookie-based server client from @supabase/ssr, which does this from the session cookie.
Jobs that run without a user
Background jobs that use the secret key must set ownership explicitly (insert ... (user_id, ...) values ('<uuid>', ...)) instead of relying on default auth.uid(), and must filter by user themselves, because RLS no longer does it for them.
Triggers see whoever caused the write
Because the claims live in the transaction, a trigger fired by an API request sees the requesting user in auth.uid(). The same trigger fired by a SQL Editor statement, a migration or a cron job sees null. Write triggers so both cases are explicit, for example new.user_id := coalesce(new.user_id, auth.uid()); followed by a clear error if it is still null, instead of silently saving an orphan row.
How to verify
From the app, while signed in, call supabase.auth.getUser() on the same client right before the failing query. If it returns a user but the query still behaves as signed out, the token is not reaching the database; if it returns no user, fix the session handling first.
Related guides
- Supabase “new row violates row-level security policy” – causes and fix
- Supabase “infinite recursion detected in policy for relation” – cause and fix
- Supabase PGRST116 “Cannot coerce the result to a single JSON object” – .single() explained
- Supabase “must be owner of table objects” – what it means and what to run instead
More in Supabase RLS and Storage · all guides