Rowpass 42501: permission denied for table Not affiliated with Supabase

Rowpass / Guides

Which policy does each command need?

One table: every command against the policies Postgres checks for it.

Policies applied by command

CommandSELECT policy (using)INSERT policy (with check)UPDATE policy (using / with check)DELETE policy (using)
selectFilters existing rowsNot usedNot usedNot used
insertChecks the new row, if the statement reads it backChecks the new rowNot usedNot used
updateFilters existing rows and checks the new row, if the statement reads columnsNot usedFilters existing rows / checks the new rowNot used
deleteFilters existing rows, if the statement reads columnsNot usedNot usedFilters existing rows
insert ... on conflict do updateChecks existing and new rowsChecks the new rowChecks the existing row / checks the new rowNot used

Condensed from Table 300 in the PostgreSQL manual, CREATE POLICY, read on the date under Sources. "Reads" means a where or returning clause that refers to the table's columns. The manual's table also covers merge and select for update.

Worked example: the insert that fails only when you ask for the row back

alter table public.notes enable row level security;
grant select, insert on public.notes to authenticated;

create policy "insert own" on public.notes
  for insert to authenticated with check ( true );
CallResultWhy
.insert(row)WorksOnly the INSERT policy is checked.
.insert(row).select()42501, new row violates row-level security policy for table "notes"Returning the row reads it, so the new row must also pass a SELECT policy. There is none.
Same call after adding a SELECT policyWorksBoth policies pass.

We ran these three steps in our test and got exactly these results.

Policy error or grant error?

MessageLayerFix
permission denied for table notesGrantGrant the role. No policy has run yet.
new row violates row-level security policy for table "notes"PolicyAdd or correct the policy for that command in the table above.

Both carry code 42501. The tool below writes the grants and the matching four policies for a table in one block.

Write the fix for your tables

One table is free. The whole schema with a rollback file is $9 one-time.

 

Waiting for a paste.

    Who can reach each table

    Your tables show up here, one row each, with a choice of who can reach them.

    The SQL appears here as soon as a table is found.

    Parsed in your browser. Your SQL is never uploaded, and no site or database is scanned.

    Questions

    Why does upsert fail when insert works?

    An upsert can take the update path, so it is checked against the UPDATE policy and the SELECT policy as well as the INSERT policy. The role also needs the UPDATE and SELECT grants.

    Does service_role need policies?

    No. Supabase documents that service_role has the BYPASSRLS attribute, so policies never apply to it. It still needs grants.

    Sources

    Related

    Did it fix your error?