Supabase returns an empty array but the rows are there
There is no error, no warning and nothing in the logs. You get
[], the rows are visibly there in the table editor, and nothing tells
you why. This is the single most confusing behaviour in Supabase, and it is
deliberate.
Why it is silent
Row Level Security does not refuse a SELECT. It filters it. Rows
you are not permitted to see are removed from the result before it reaches you,
exactly as if they did not exist. There is nothing to report, so nothing is
reported.
That has an implication worth sitting with: a blocked row and a missing row are indistinguishable from the client. Your app cannot tell the difference, and neither can you by looking at the response.
The same property runs in the other direction, which is the part that costs people money. A table with no policy at all is also silent. Nothing warns you that every row is reachable by anyone. Silence is the default in both states.
Isolate it in three steps
1. Find out who the database thinks you are
Most empty arrays are an absent session rather than a wrong policy. Run this in the client that is actually failing, not in a different one:
const { data: { user } } = await supabase.auth.getUser();
console.log('the database will see auth.uid() =', user?.id ?? 'null');
If that prints null, every policy comparing
auth.uid() to a column is false and the correct result is an empty
array. This is routine in server-rendered pages, where the browser holds a session
and the server client was created without one.
2. Read the policy that applies
select policyname, cmd, roles, qual as using_clause, with_check from pg_policies where schemaname = 'public' and tablename = 'invoices';
Two things to check. Whether any policy covers SELECT at all, and
whether its roles column includes the role you are actually using. A
policy written to authenticated does nothing for a logged-out
visitor, and one written to anon does nothing for a logged-in one.
The two roles do not inherit from each other.
3. Confirm from outside, with your own key
curl "https://YOUR-PROJECT.supabase.co/rest/v1/invoices?select=*&limit=5" \ -H "apikey: YOUR_ANON_KEY"
This is the request an anonymous visitor makes. If it returns rows, your table
is readable by anyone and the empty array in your app has a different cause. If it
returns [], the policy is doing its job and the problem is the
session.
The causes, in the order they actually occur
- No session.
auth.uid()is null, so ownership checks fail. Most common by a distance. - Row Level Security enabled with no policy. Enabling it denies everything until something allows it. An empty table is the expected state in between.
- The policy is granted to the wrong role. See step 2.
- The owner column is null on the existing rows. Rows created before the
column existed will never match
auth.uid() = user_id. - It used to use the service role key. During prototyping the client was built with a key that bypasses policies. Swapping to the anon key subjects it to them for the first time, and everything goes quiet.
What not to do
Disabling Row Level Security makes the rows come back and makes the table readable by anyone holding your anon key, which is in your bundle. It converts a confusing bug into a silent exposure, and the silence is the same, so you will not notice.
Likewise using (true) makes the query work and makes the data
public. If you write it deliberately for reference data, restrict it to
for select and say so in a comment. If you write it to make an error
go away, you have removed the protection rather than fixed the query.
Prove it rather than assume it
Because the failure mode is silence, the only reliable check is an explicit one: sign in as one user, create a row, then sign in as a different user and confirm that the row is not visible, and that they cannot update or delete it. Anything less and you are trusting a dashboard shield that does not tell you what your policies actually do.
Related
- "new row violates row-level security policy"
- Is my Supabase database public?
- NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY
- RLS Disabled in Public
- Is it safe to expose the Supabase anon key?
- Supabase security scanner
Check your own project
One command. No install, no signup, and it never touches the network. It reads your repository and tells you which tables are reachable and which keys are exposed, with the fix beside each finding.
npx github:humora2504/vibeproof .
Free and MIT licensed. If you want the policies generated for your own schema, and a script that proves an anonymous visitor is refused, there is a $19 Fix Pack. The scanner stays free either way.