Dev.to Security 🔐 Cybersecurity 👁 0 📖 7 min read

Fix Supabase "permission denied for table" on new tables (42501)

You add a feature, your AI builder creates a new table for it, and the feature fails. The old screens still work. The browser console shows this: { "code": "42501", "message": "permission denied for table invoices

You add a feature, your AI builder creates a new table for it, and the feature fails. The old screens still work. The browser console shows this:

{
  "code": "42501",
  "message": "permission denied for table invoices",
  "hint": "Grant the required privileges to the current role with: GRANT SELECT ON public.invoices TO anon;"
}

If your Supabase project is older and this started recently, there is a good chance it's not a bug in your code. Supabase changed how new tables are exposed, and its schedule applied the change to all existing projects on 30 October 2026. Below is what changed, how I confirm it's a missing grant, and the SQL I use to fix it without opening the table to everyone.

It's based on Supabase's changelog post on the change, the Securing your API guide, the knowledge base article on Database API 42501 errors and the PostgREST error reference, all read on 21 November 2026.

What changed on 30 October

Before the change, a Supabase project granted select, insert, update and delete on every new table in the public schema to the anon, authenticated and service_role roles. Create a table and it was reachable from supabase-js right away.

Supabase's changelog explains why that's gone: "agents, CLI scripts, and AI platforms create tables too, and many of those operations do not have a human reviewing the diff." The timeline in the same post:

  • 28 April 2026: an opt-in setting at project creation.
  • 30 May 2026: the new behavior becomes the default for new projects.
  • 30 October 2026: it is applied to all existing projects.

Now a new table in public needs an explicit Postgres grant before the Data API can reach it. Tables that existed before the change keep their grants, which is why your old screens still work and only the new feature breaks. The changelog also says it doesn't matter how the table was created: SQL editor, migrations, the CLI, MCP or "an AI coding tool".

If your app talks to Postgres over a direct connection (an ORM or a server with a connection string), this change doesn't affect you. It applies to the Data API, which is what supabase-js calls.

Step 1: confirm it's a missing grant

42501 is Postgres's insufficient-privilege code, so it covers more than one problem. PostgREST turns it into HTTP 403 for a signed-in user and 401 otherwise, which can send you looking for an expired session first.

Look at the message. A missing grant says permission denied for table your_table, and the hint names the role and privilege that were missing. Then check what the table actually grants, with the query from Supabase's 42501 article, in the SQL Editor:

select grantee, privilege_type
from information_schema.role_table_grants
where table_name = 'invoices';

If anon and authenticated aren't in the result, you've found it. To see whether your project now creates tables without grants, the Securing your API guide has a query over pg_default_acl; when it returns no rows, "new objects grant nothing to the Data API roles until you grant access explicitly."

Step 2: grant only what each role needs

The hint gives you a GRANT statement. Don't paste it blindly. It shows what this one request needed; whether that role should have it is your call.

For a typical table where each signed-in user manages their own rows:

-- signed-in users; RLS still decides which rows
grant select, insert, update, delete on table public.invoices to authenticated;

-- only if signed-out visitors should read this table
-- grant select on table public.invoices to anon;

-- only if server code with the secret key uses this table
grant select, insert, update, delete on table public.invoices to service_role;

Most private app tables need nothing for anon. The service_role line matters more than people expect: the change revokes that default as well, so an Edge Function that uses the secret key can get the same error on a new table. Supabase's API keys guide says the secret key "bypasses Row Level Security", but row security and grants are separate layers, and bypassing one doesn't bypass the other.

One more case: if the table's id is an old-style serial column, its default calls nextval(), and the Postgres docs say that "requires USAGE or UPDATE privilege on the sequence". An insert that fails with permission denied for sequence invoices_id_seq needs grant usage on sequence public.invoices_id_seq to authenticated;.

Step 3: turn on RLS in the same change

This is the step that keeps the fix from becoming a leak. A grant lets a role reach the table. Row Level Security decides which rows it gets. Without RLS, the Securing your API guide warns, an exposed table "can be accessed by any role with matching grants", and your publishable key is in the browser by design.

Supabase's changelog puts it plainly: "Treat these three steps as a unit." Grant, enable RLS, add policies:

alter table public.invoices enable row level security;

create policy "Users read their own invoices"
on public.invoices for select
to authenticated
using ( (select auth.uid()) = user_id );

create policy "Users create their own invoices"
on public.invoices for insert
to authenticated
with check ( (select auth.uid()) = user_id );

Add update and delete policies only if the app needs them. Tables created in the Dashboard get RLS on by default; tables created in the SQL Editor or by another tool need it turned on, so check every table your AI tool made.

Don't fix it with grant ... on all tables in schema public. The changelog shows that bulk statement as a rollback, but it hands every table in public to every role, including tables nobody meant to expose. Grant per table.

Step 4: make your AI tool write the grant

If Lovable, Bolt, Cursor or another tool writes your migrations, the error will come back with the next table unless the grant is part of how tables get created. The changelog suggests updating the tool's system prompt or using Supabase's agent skill. In practice, I'd add a project instruction along these lines:

Every new table in public must be created in one migration with: explicit grants for the roles that need it (authenticated, and anon only if public), enable row level security, and policies for each operation the app uses.

Then read the migration before you approve it. Three blocks: grants, RLS, policies. If one is missing, ask for it.

The same applies when you replay old migrations into a fresh project, such as a staging copy or a move off a builder's hosted backend. A commenter in Supabase's discussion of the change points out that migrations written when grants were implicit bring every table up unreachable. The fix is one catch-up migration, placed after every create table, with the per-table grants.

Not every 42501 is a grant

The same code appears for other reasons, and the fix is different:

  • new row violates row-level security policy: the grant is there, but no policy lets the row in. That's an RLS problem (link below).
  • Column-level restrictions: Supabase's 42501 article notes that select * fails if it expands to a column the role can't read.
  • The auth and vault schemas: API roles can't access them, on purpose.

If it says "schema cache" instead

A new table can also fail with Could not find the table 'public.invoices' in the schema cache, code PGRST205. The PostgREST error reference says that one is "caused when the table specified in the URI is not found". It isn't about grants. Check that the migration ran in the project your app actually uses (compare the project URL in your environment variables), that the name and schema match, and if the table really is there, reload the cache with Supabase's documented command:

notify pgrst, 'reload schema';

Prove it works

  1. Sign in as a normal test user and use the new feature.
  2. Sign out and try again: you should get your app's signed-out path, not a 42501.
  3. Use two test users and confirm each sees only their own rows.
  4. Check the Data API exposure badge in the Table Editor and the Security Advisor, which the changelog names as the places to review exposed tables.

If step 3 shows someone else's data, stop and fix the policies before shipping.

If your inserts fail with new row violates row-level security policy once the grant is in, I wrote that fix up here: Fix "new row violates row-level security policy" in Supabase. And if your AI tool has created a lot of tables and you'd like a second look at the code, the free AI app audit reports findings with the file and line.

I wrote this with AI assistance and reviewed it myself.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.