Category: Supabase and databases

Supabase: SELECT returns empty results despite valid authorization — RLS diagnostic

Read this guide in Polish

Problem: the user sees an active session in the Supabase client, token is present, but SELECT on the table returns empty results or 0 lines. The reasons are usually in the RLS configuration (no policy, incorrect expression, USING/ WITH CHECK, roles anon/auth) or in the authentication layer (token does not contain expected claims, incorrect audience/issuer, or request performed as service role). This document runs through controlled tests: role confirmation, policy inspection, SQL editor simulation of the query, JWT claim check and indicates specific corrections and minimum verification test before implementation of changes.

How to Recognize Cause

Check if the table has RLS enabled: in Dashboard → Table Editor → the Policies tab is active. If RLS is enabled and there is no policy for SELECT shares, all normal roles (auth/anon) will get 0 results. Common mistake: to disable RLS instead of adding the right rules.

Verify policy content: SELECT policy can have a clause USING or expression checking e.g. auth.uid() = owner id. Error in column name, type or operator (e.g. = instead of IS NOT DISTINCT FROM) results in rejection of the line. The difference between USING (filter) and WITH CHECK (for INSERT/UPDATE) is critical.

Check the query context: the client token must be transmitted and contain a valid sub (auth.uid()) or custom claim used in the policy. If you make an inquiry from the server using service role, RLS is disabled and the results will be different from the customer. Make sure the request comes from the same token you're watching.

Step by step solution

1. Confirm authorization: in the browser console, check localStorage/sessionStorage key "supabase.auth.token" or call supabase.auth.getSession() and save returned access token; do not assume that UI presence = valid Token.

2 . Open SQL Editor in Supabase and perform SELECT using auth.uid(): SELECT auth.uid(), current setting('request.jwt.claims', true); — check whether auth.uid() returns the expected id. If NULL, token is not sent correctly.

3 . Check policies: go to Table Editor → Policies; for SELECT check USING expression. For the test, temporarily add a simple policy allow for your role: USING (true) — this will determine whether the problem is due to the rules.

4 . Test client query vs SQL Editor: compare results when you perform the same SELECT in SQL Editor (made in the context of the dashboard) and from the browser application (using access token). Differences will indicate a problem with token or with headers.

5 . Examine JWT: decode access token (base64) and compare claim 'sub' or custom claim required in policy. Make sure the issue/audience corresponds to the design configuration. If claim doesn't exist, the police won't let the lines go.

6 . Check roles used in policy: many examples use roles() = 'authenticated' or current setting('request.jwt.claims.role', true). If the policy refers to a custom role field, confirm that it is present in JWT.

7 . Restore safe policy: after diagnostics replace the temporary USING(true) with a precise rule e.g. USING (auth.uid() = owner id) and perform tests with a token containing a sub corresponding to owner id.

How to see if the problem is gone

Test A: Sign in to the application as a test user, execute SELECT from the customer. Expected result: returned lines having a Owner id equal to auth.uid().

Test B: In SQL Editor, perform the same SELECT by simulating the dashboard (without the RLS change) and compare; if SQL Editor returns rows and the client does not—a problem with token or headers.

Test C: When you restore the correct policy, remove the temporary USING(true) and repeat Test A; if you fail, check the server logs and decode token at the time of request.

What to do

Do not leave USING(true) permanently in production — opens access to all lines.

Do not diagnose after Dashboard SQL Editor; the results there may differ from the client due to another RLS context.

Do not edit policy without backup; copy the existing definition before modification.