Skip to content
>_VibeUnstuck

new row violates row-level security policy

Last verified:

The fix

new row violates row-level security policy: RLS is on and no INSERT policy allows this role. Add a policy for the signed-in user. A missing GRANT differs.

  1. 1

    See which policies exist

    In the Supabase SQL editor, list policies for the table named in the error. An empty result means every insert is denied.

    select policyname, cmd, roles, qual, with_check
    from pg_policies
    where schemaname = 'public'
      and tablename = 'todos';
  2. 2

    Check the role on the request

    The public client key acts as anon until a user session is attached, then as authenticated. In the browser network panel, a request with only the apikey header and no Authorization: Bearer user JWT is anon. A policy written to authenticated will not allow it.

  3. 3

    Add an insert policy for rows the user owns

    For a table with a user_id column, allow signed-in users to insert only their own rows. The client must send user_id equal to the session user. Do not use with check (true) on private data.

    create policy "users insert own rows"
    on public.todos
    for insert
    to authenticated
    with check (auth.uid() = user_id);
  4. 4

    Keep the service role on the server

    The service role key bypasses RLS. That is for a trusted server job, never for a browser bundle, a Vite app, or a NEXT_PUBLIC_ / VITE_ variable. If the insert is supposed to happen before login, decide that explicitly and write a narrow anon policy, or insert from a server route after you have checked the user yourself.

Why it happens

Row Level Security is enabled on the table, and the insert did not pass any INSERT policy. A table with RLS and no policies denies every row for the anon and authenticated roles.

AI tools often turn RLS on in a migration, then insert from the browser with the public client key before a policy exists. If the policy checks auth.uid() = user_id and there is no signed-in user, auth.uid() is null and the check fails with the same error. Postgres uses code 42501 for this denial and also for a missing GRANT. A missing GRANT says permission denied for table, not new row violates row-level security policy. Match the message. A missing table is a different error again.

The next error you'll probably hit

Once this is fixed, the usual follow-up is relation "public.profiles" does not exist. relation "public.profiles" does not exist (42P01): that table is missing here. PGRST205 means the schema cache lacks it. Run the migration on this project.

Prompt to paste back into your AI tool

Paste this above the error.

The insert fails with: new row violates row-level security policy for table "TABLE".

Do not disable RLS. Do not put the service role key in client code or in a NEXT_PUBLIC_ / VITE_ variable.

1. Show the policies on this table and whether the failing request is role anon or authenticated.
2. If rows are owned by users, add an INSERT policy for role authenticated with CHECK (auth.uid() = user_id), and set user_id from the signed-in session on the insert.
3. If there is no session, say so. Do not paper over it with a permissive policy.
4. Paste the SQL and the client change only. Do not rewrite unrelated files.

Common questions

+Why does the insert work in the SQL editor and fail in the app?

The SQL editor usually runs as a privileged role that bypasses RLS. The app uses the anon or authenticated role, which has to pass a policy.

+The code is 42501 but the message is permission denied for table. Is this the same bug?

No. That message means the role has no GRANT on the table. This page is only the policy error, which says a new row violates row-level security.

+Will a SELECT policy fix this insert?

No. INSERT policies use WITH CHECK, not USING. A select policy can still be required later so the app can read the row it just wrote.

Related errors