Supabase 42501 "permission denied for table": what it means, and why it is not the RLS error
You run a query from your app and Supabase answers: {"code":"42501","message":"permission denied for table todos"} This is not the same error as "new row violates row-level security policy". Knowing the differen
You run a query from your app and Supabase answers:
{"code":"42501","message":"permission denied for table todos"}
This is not the same error as "new row violates row-level security policy". Knowing the difference saves an hour, and the wrong fix can open your whole database.
What 42501 "permission denied for table" means
Postgres has two separate gates:
- Table privileges (GRANT). Can this database role touch this table at all?
- Row-level security (RLS policies). Which rows can that role see or change?
permission denied for table comes from gate 1. The role your request runs as (anon for a logged-out visitor, authenticated for a signed-in user) has no GRANT on the table. RLS policies are never even reached.
"new row violates row-level security policy" comes from gate 2. The role has the privilege, but no policy allows that row.
The 4 usual causes
- The table was created with SQL, not the dashboard. Tables made through the dashboard get default grants. Tables made by a raw migration or a script may not.
-
You are calling it as
anonwhen you meant to be signed in. The session expired, or the client was created without the user's token. Check which role the request really used. -
A schema you created yourself has no grants for
anonorauthenticated, and the API role cannot use the schema. - A view or function runs with the caller's privileges and the underlying table is not granted.
The fix, in order
First, find out the role. In the SQL editor, run select current_user; from the same path your app uses, or log the JWT role claim from the failing request.
Then grant only what that role needs:
grant usage on schema public to authenticated;
grant select, insert, update, delete on public.todos to authenticated;
Keep RLS on and write policies so users only reach their own rows:
alter table public.todos enable row level security;
create policy "own rows" on public.todos
for all to authenticated
using (user_id = auth.uid())
with check (user_id = auth.uid());
The fix that makes things worse
Do not run grant all on all tables in schema public to anon; to make the error go away. Together with RLS disabled, that gives every visitor full read and write on your data. It is the most common way an AI-built app ends up with an open database.
Check you did not open it by accident
After fixing, test from the outside. Request the table with only your public anon key and no login. You should get an empty list or a permission error, not rows. A free read-only check of what your deployed app exposes to every visitor is at https://proofforge.live, and a free test for the harder question, whether user A can read user B's rows, is at https://proofforge.live/two-account-test
Paid one-time fix reports are $9, $29 and $79, and the free scan does not require them.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.