"new row violates row-level security policy"

This error means the database stopped you writing a row that no policy allowed. It is not a bug. It is the one moment Row Level Security tells you it is switched on, and it is worth understanding rather than switching off.

Here is the exact shape of it:

{
  "code": "42501",
  "message": "new row violates row-level security policy for table \"posts\""
}

What the database is actually checking

Every write is tested against a policy. The two halves matter, and mixing them up causes most of these errors:

An INSERT has no existing row, so USING is never consulted. If your policy only has a USING clause, every insert fails, however correct that clause looks.

The four causes, in the order you should check them

1. The policy has no WITH CHECK

This is the most common cause by a wide margin.

-- Allows reading your own rows. Allows inserting nothing at all.
create policy "own posts" on public.posts
  for all to authenticated
  using ( (select auth.uid()) = user_id );

-- Fixed: the same condition applied to rows being written.
create policy "own posts" on public.posts
  for all to authenticated
  using ( (select auth.uid()) = user_id )
  with check ( (select auth.uid()) = user_id );

2. You are not sending the row's owner

The policy compares auth.uid() with a column. If your insert leaves that column null, the comparison is false and the write is refused.

// Refused: user_id is null, so it cannot equal auth.uid()
await supabase.from('posts').insert({ body });

// Allowed
const { data: { user } } = await supabase.auth.getUser();
await supabase.from('posts').insert({ body, user_id: user.id });

Better still, let the database fill it in and stop trusting the client:

alter table public.posts
  alter column user_id set default auth.uid();

3. There is no session, so auth.uid() is null

If the request carries no valid token, auth.uid() returns null and every ownership check fails. This catches people out with server-rendered pages, where the browser has a session but the server client was never given it.

Check what the database sees, from the client that is actually failing:

const { data: { user }, error } = await supabase.auth.getUser();
console.log('database will see uid:', user?.id ?? 'null');

4. The policy is granted to the wrong role

A policy written to authenticated does nothing for a logged-out visitor, and one written to anon does nothing for a logged-in one. Roles are not inherited between the two.

-- Applies to both, when that is what you mean
create policy "own posts" on public.posts
  for all to anon, authenticated
  using ( (select auth.uid()) = user_id )
  with check ( (select auth.uid()) = user_id );

The fifth cause, which is not a policy problem at all

Row Level Security is checked after table privileges, not instead of them. If the role has no INSERT privilege, no policy can rescue it:

grant select, insert, update, delete on public.posts to authenticated;

People hit this after copying a hardening snippet that revokes everything from anon and forgets to grant anything back to authenticated.

How to see which policy refused you

List what is actually on the table, rather than what you think you wrote:

select policyname, cmd, roles, qual as using_clause, with_check
from pg_policies
where schemaname = 'public' and tablename = 'posts';

A with_check column showing null on a row where cmd is ALL or INSERT is your answer.

What not to do

The two fixes that show up in forum answers and make things worse:

alter table posts disable row level security makes the error stop and makes the table readable and writable by anyone with your anon key. That key is inside your frontend bundle, so that means anyone at all.

with check (true) stops the error and lets any logged-in user write rows owned by anyone else, which is usually worse than it sounds: it means one user can create records attributed to another.

Testing it properly

The SQL editor runs as a privileged role and bypasses policies entirely. A query that works there tells you nothing. Test with an ordinary client, signed in as a real user, and then test again signed in as a different user to confirm they cannot reach the first one's rows.

Related


Check your own project

One command. No install, no signup, and it never touches the network. It reads your repository and tells you which tables are reachable and which keys are exposed, with the fix beside each finding.

npx github:humora2504/vibeproof .

Free and MIT licensed. If you want the policies generated for your own schema, and a script that proves an anonymous visitor is refused, there is a $19 Fix Pack. The scanner stays free either way.