← All errors · SupabaseSupabase database & RLS errors
new row violates row-level security policy for table
Exact error
new row violates row-level security policy for table "todos"
What it means
Row Level Security is enabled on the table and no policy allows the current user to insert (or update) this row. Postgres error code 42501.
Likely causes
- RLS enabled with no INSERT policy.
- Policy WITH CHECK condition fails, e.g. user_id not set to auth.uid().
- Request made with the anon key and no logged-in session.
How to fix it
Add an insert policy for authenticated users that own the row:
create policy "Users can insert their own todos" on public.todos for insert to authenticated with check ( (select auth.uid()) = user_id );Make sure the insert sets user_id (or give the column a default):
alter table public.todos alter column user_id set default auth.uid();Confirm the client actually has a session (supabase.auth.getUser()) before inserting.
Never "fix" this by using the service_role key in the browser.
Prevent this next time
- Keep schema and RLS policies in migrations (`supabase migration new`) instead of editing production by hand.
- Test each policy as both an anonymous and a signed-in user before shipping.
FAQ
What does "new row violates row-level security policy for table" mean?
Row Level Security is enabled on the table and no policy allows the current user to insert (or update) this row. Postgres error code 42501.
What usually causes it?
RLS enabled with no INSERT policy. Policy WITH CHECK condition fails, e.g. user_id not set to auth.uid(). Request made with the anon key and no logged-in session.
How do I fix it?
1. Add an insert policy for authenticated users that own the row: 2. Make sure the insert sets user_id (or give the column a default): 3. Confirm the client actually has a session (supabase.auth.getUser()) before inserting. 4. Never "fix" this by using the service_role key in the browser.
Related errors
Get new fixes weekly
No spam. Unsubscribe anytime. Privacy