← Debugging Guides

阅读中文版本

Supabase — RLS policy blocks insert with 42501

Supabase returns 42501 on insert because Row Level Security is enabled but no policy allows the authenticated user to insert into the table.

intermediatedebuggingsupabaserls

Symptoms

  • error: 42501 - insufficient privilege
  • Insert works with service_role key but not with anon or authenticated
  • Select works but insert fails

Possible causes

  • RLS is enabled but no INSERT policy exists for the table
  • The policy condition does not match the user role or ID
  • The policy references a column the user cannot access

Diagnosis steps

Check RLS status and policies

supabase db dump --schema public | grep -A5 "policy"

Interpretation

Confirm RLS is enabled and list existing policies for the table.

Test the policy condition

supabase sql --query "SELECT auth.uid()"

Interpretation

Verify the auth context matches what the policy expects.

Root cause

Row Level Security is enabled on the table, but no policy grants INSERT permission to the authenticated role, so Postgres rejects the operation.

Fix

Create an INSERT policy that allows the authenticated user to insert rows, e.g. CREATE POLICY "users insert" ON table FOR INSERT TO authenticated WITH CHECK (true);

Verify the fix

Retry the insert with the authenticated user token and confirm it succeeds; verify a user without the policy cannot insert.

Symptom walkthrough

Supabase returns 42501 on insert because Row Level Security is enabled but no policy allows the authenticated user to insert into the table. The error appears in production but may not surface in local development because the conditions that trigger it depend on the deployment environment or runtime configuration. Understanding the full symptom set helps distinguish this issue from similar errors.

Diagnosis in practice

Follow the diagnosis steps in the frontmatter to isolate the cause. Each step includes a command and an interpretation of the output. The goal is to narrow down whether the issue is in the configuration, the runtime, or the application code. Reproduce the error in a minimal setup before applying a fix.

Root cause

Row Level Security is enabled on the table, but no policy grants INSERT permission to the authenticated role, so Postgres rejects the operation.

Fix

Create an INSERT policy that allows the authenticated user to insert rows, e.g. CREATE POLICY “users insert” ON table FOR INSERT TO authenticated WITH CHECK (true); After applying the fix, monitor the system to confirm the error does not recur under load or in different environments.

Verification

Retry the insert with the authenticated user token and confirm it succeeds; verify a user without the policy cannot insert. If the verification passes, the fix is confirmed; if not, re-examine the diagnosis steps for missed contributing factors.

Diagnostic commands

Run these commands to reproduce and diagnose the issue:

supabase db dump --schema public | grep -A5 "policy"
supabase sql --query "SELECT auth.uid()"