← All guides

Supabase "new row violates row-level security policy": causes and fixes

SupabaseLovableBolt

You click Save, Sign up or Upload, and nothing happens. In the browser console or the network tab, the response from Supabase says something like:

new row violates row-level security policy for table "profiles"

usually with the Postgres error code 42501. The table name changes (profiles, orders, tasks, or objects for file uploads), but the meaning is the same: the database looked at the row your app tried to write, checked it against the table’s Row Level Security (RLS) rules, and refused.

That’s RLS doing its job. The fix is almost never to switch it off. It’s to make the request and the policy agree. This guide explains what the error means, how to find which rule is failing, five common causes in apps built with Lovable and Bolt, and paste-ready policies for the common case.

What the error actually means

RLS lets a table decide, row by row, what each user may read or write. Policies use two kinds of condition:

  • using decides which existing rows a user can see, update or delete.
  • with check decides whether a new or changed row is allowed. Supabase’s docs describe it as ensuring “any new row adheres to the policy constraints, so a user cannot create a row that belongs to someone else” (Supabase RLS docs).

PostgreSQL’s own documentation explains the two outcomes. Rows that fail a using check are usually just hidden, with no error. Rows that fail a with check check raise an error (PostgreSQL CREATE POLICY). So “new row violates…” means one of two things:

  1. An insert or update produced a row that no policy allows. Either there’s no policy for that operation, or the with check condition came out false.
  2. The write asked for the row back, and no SELECT policy lets the user see it. PostgreSQL says that when a write has a RETURNING clause, the new rows “must satisfy the relation’s SELECT policies”. If they don’t, the whole statement fails, even though the insert itself was allowed.

The second case catches a lot of people, because the INSERT policy looks perfect.

Find which table and operation failed

Before changing anything, collect three facts:

  • The table. It’s in quotes at the end of the message. "objects" means Supabase Storage (storage.objects), not one of your own tables.
  • The operation. Open the browser’s developer tools, go to the Network tab and repeat the action. Click the request that shows an error status. The method and URL show whether it was an insert (POST), an update (PATCH) or an upsert (a POST with a conflict setting in the URL or headers). The request body shows the exact row your app sent.
  • Who was calling. Check whether the user was signed in. A signed-out request runs as the anon role; a signed-in one runs as authenticated.

Then open your project’s policies in the Supabase dashboard (or in Lovable’s database view if your app uses its built-in backend) and read the policies on that table for that operation.

Cause 1: no INSERT policy, or a WITH CHECK that doesn’t match

This is a common case in AI-built apps. The tool writes the insert code in one step and the policy in another, and the two drift apart.

Typical versions:

  • There’s a SELECT policy but no INSERT policy. Users can read rows, but nobody can add one.
  • The policy compares auth.uid() to user_id, but the app never sends user_id. The column is empty, so auth.uid() = user_id isn’t true, and the row is rejected.
  • The app sends the wrong value. For example, a profile ID or a hard-coded test ID instead of the signed-in user’s ID.
  • The column is named differently from what the policy expects (owner_id in the table, user_id in the policy).

Fix: make sure every insert sets the owner column to the signed-in user’s ID, and that an INSERT policy exists with a matching check. Another option is a column default of auth.uid(), so the database fills in the owner when the app leaves it out. The policy still does the enforcing.

Cause 2: insert followed by .select(), with no matching SELECT policy

In supabase-js v2, inserts don’t return the new row by default. The library’s insert reference says “By default, inserted rows are not returned”, and you add .select() to get it back (supabase-js insert; supabase-js v2 announcement). AI tools add .select() all the time, because they want the new ID to update the screen.

That turns the write into one that needs to read the row back, so the SELECT policy now matters too. If the user can insert a row but isn’t allowed to see it, the request fails with the same “new row violates” message.

Fix: add a SELECT policy that lets the owner read their own rows, or remove .select() if the app doesn’t need the row back.

The same applies to updates. Supabase’s docs warn that “to perform an UPDATE operation, a corresponding SELECT policy is required.”

Updates and upserts

  • Update: the using clause picks which existing rows the user can change, and with check decides what the changed row may look like. If the app changes user_id to someone else, or the with check condition doesn’t hold after the change, you get this error. If there’s no with check, the using expression is applied to both (PostgreSQL CREATE POLICY docs).
  • Upsert: an upsert can insert or update, so it can need INSERT, UPDATE and SELECT policies, depending on whether the row already exists. PostgreSQL’s CREATE POLICY page covers how ON CONFLICT DO UPDATE is checked.

Cause 3: the user isn’t signed in

Supabase has two client roles: anon for “an unauthenticated request (the user is not signed in)” and authenticated for a signed-in one (Supabase RLS docs). A policy written to authenticated doesn’t apply to anon requests at all.

This shows up when:

  • the insert runs before sign-in finishes, or on a page that loads before the session is restored
  • the session expired and the app didn’t notice
  • a server function calls Supabase without passing the user’s session, so it runs as anon

Fix: check the session before writing, and pass the user’s auth token in server-side calls. Don’t solve it by adding a policy for anon unless signed-out visitors are genuinely meant to write to that table.

Cause 4: Storage uploads

If the table in the message is "objects", the failing rule is on storage.objects, the table Supabase Storage uses to track files. Supabase’s docs say “by default Storage does not allow any uploads to buckets without RLS policies” (Storage access control).

Check, in order:

  • Is there an INSERT policy on storage.objects for this bucket? For a plain upload, the docs say that’s the only policy required.
  • Does the path match the policy? Many policies only allow uploads into a folder named after the user. Supabase’s example checks that (storage.foldername(name))[1] equals the user’s ID. If the app uploads to avatars/photo.png instead of <user-id>/photo.png, the check fails.
  • Is the upload using upsert: true? Supabase’s storage troubleshooting article says this error on upload happens specifically when upsert is true. Overwriting needs the database to read the file’s row back, so you also need a SELECT policy (and UPDATE, per the access control docs). If you don’t need to overwrite files, leave upsert at its default of false. A duplicate path then returns a 400 “Asset already exists” error instead (Storage 403 troubleshooting).

Cause 5: a missing grant vs a policy failure

Grants and policies are separate. Supabase’s docs put it this way: grants decide whether a role can run an operation on the table at all, and policies decide which rows it applies to. They also say “a missing grant raises a 42501 error before any policy runs” (Supabase RLS docs).

So 42501 alone doesn’t prove it’s a policy. If the message mentions a row-level security policy, look at your policies. If it doesn’t, check whether the authenticated role has been granted insert (or update) on the table. Not every project grants this automatically, according to the same docs.

Copy-paste fix: owner INSERT and SELECT policies

For a table where each row belongs to one user through a user_id column, these policies follow the pattern in Supabase’s docs. Replace profiles with your table name, and check that your owner column really is called user_id.

alter table public.profiles enable row level security;

create policy "Owners can insert their rows"
on public.profiles for insert
to authenticated
with check ( (select auth.uid()) = user_id );

create policy "Owners can read their rows"
on public.profiles for select
to authenticated
using ( (select auth.uid()) = user_id );

If your app also updates rows, add an UPDATE policy with both using and with check, as shown in Supabase’s docs. For the full set of owner, public-read, profiles and storage templates, see Supabase RLS policies explained. Then test as two different accounts: each should see and create only their own rows.

If your project doesn’t grant table privileges automatically, also grant the operations the authenticated role needs. The Supabase RLS guide shows the exact grant statements.

A prompt you can give Lovable or Bolt

If you’d rather have the AI tool make the change, be specific so it doesn’t take a shortcut:

Inserting into the profiles table fails with “new row violates row-level security policy”. Do not disable RLS and do not use the service role key in the frontend. Make sure every insert sets user_id to the signed-in user’s ID, add an INSERT policy with with check (auth.uid() = user_id) and a SELECT policy with using (auth.uid() = user_id) for the authenticated role, and show me the SQL before applying it.

Read the SQL it proposes. If it contains using (true) or with check (true), or a policy for anon, ask why.

What not to do

  • Don’t disable RLS to make the error go away. Supabase’s docs warn that a table in an exposed schema without RLS “is readable and writable by any role with a grant on it”. That can mean anyone with your public anon key, which is in your app’s code.
  • Don’t replace the policy with true. A with check (true) policy lets any user write any row, including rows that claim to belong to someone else.
  • Don’t put the service_role key in the frontend. It “bypasses RLS, so keep it server-side” (Supabase RLS docs). In the browser, anyone can copy it and get full access to your database.

When to get a human to check every table

One error message is easy to fix. The harder question is what the rest of your policies look like. This error tells you a policy is too strict. It says nothing about the tables where policies are too loose, which fail silently by letting the wrong people in. Run Supabase’s database advisors, work through our vibe-coded app security checklist, and test every table with a second account.

If you’d like someone to do that review for you, inahei.pro pairs you with vetted senior engineers who review and fix AI-built apps and show you what went wrong. Fixed price, agreed upfront. Experts sign NDAs and use scoped tokens and collaborator invites, never shared passwords. See /fix/supabase, /fix/lovable or /fix/bolt.

FAQ

Why do I get this error when my INSERT policy looks correct?

Usually because the request also reads the row back. If your code chains .select() after an insert, or you use an upsert, the new row also has to pass a SELECT policy. Add an owner SELECT policy, or drop .select() if you don’t need the row. Also check that the app actually sends user_id: an empty owner column fails a correct-looking policy.

How do I fix it for Supabase Storage uploads?

Add an INSERT policy on storage.objects for your bucket, and make sure the upload path matches any folder rule (for example, the first folder must be the user’s ID). If you upload with upsert: true, Supabase says you also need SELECT and UPDATE policies. Otherwise, use the default upsert: false.

Is it safe to just disable RLS on the table?

No. Without RLS, any role with a grant on the table can read and write every row. In a typical Supabase app, that includes visitors using your public anon key.

Why does it work in the Lovable preview but fail for real users?

Common reasons: you were signed in during testing but real users hit the page before signing in (so they run as anon); your test account owns the rows it’s reading; or the live app is connected to a different database with different policies. Repeat the action in a private window with a brand-new account and compare the request in the Network tab.

Should I use the service_role key to get around it?

Not in the browser, ever. On the server, the service role key is meant for trusted admin tasks, and it skips RLS completely. If you use it there, your code becomes responsible for checking that each user may only touch their own rows. For normal user actions, it’s safer to call Supabase as the signed-in user and let the policies do that work.

Comments

Add a comment

Generated for you. Click to type your own.

Required. Never shown on the page. Privacy policy.

Basic markdown: **bold**, *italic*, `code`, lists, quotes, and fenced code blocks. HTML shows as plain text.

Your draft stays on this device until you post.