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
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
Check the role on the request
The public client key acts as
anonuntil a user session is attached, then asauthenticated. In the browser network panel, a request with only theapikeyheader and noAuthorization: Beareruser JWT isanon. A policy writtento authenticatedwill not allow it. - 3
Add an insert policy for rows the user owns
For a table with a
user_idcolumn, allow signed-in users to insert only their own rows. The client must senduser_idequal to the session user. Do not usewith 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
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 narrowanonpolicy, 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
- relation "public.profiles" does not existrelation "public.profiles" does not exist (42P01): that table is missing here. PGRST205 means the schema cache lacks it. Run the migration on this project.CursorLovableBoltSupabasePostgresOct 19, 2026
- null value in column violates not-null constraintnull value in column "user_id" of relation "todos" violates not-null constraint. Postgres 23502. The insert omitted a required column. Send it, or give the column a default. Do not drop NOT NULL to hide a missing value.CursorLovableBoltSupabasePostgresOct 27, 2026
- permission denied for table (42501)permission denied for table (42501): the Supabase role has no GRANT. Postgres stops before Row Level Security. Grant that command and leave RLS on.CursorLovableBoltSupabasePostgresOct 15, 2026
- Invalid API keyInvalid API key. Supabase received a key this project does not accept. Copy the publishable key, or the legacy anon key, from the same project as the URL. No API key found in request means the header was missing.CursorLovableBoltSupabaseNext.jsNov 6, 2026
- Auth session missing after refreshAuth session missing: sign-in worked in the browser, but Next.js cannot see the Supabase cookie. Use the @supabase/ssr server client and refresh.CursorLovableBoltSupabaseAuthOct 14, 2026
- NEXT_PUBLIC_ secret in the bundleA NEXT_PUBLIC_ or VITE_ secret is in the browser bundle. Rotate the key, move the call to a server route, and keep real public keys in the client.CursorLovableBoltNext.jsViteNov 11, 2026
- infinite recursion detected in policy for relationA Row Level Security policy queried a table whose policy needs the first policy again. Postgres stops it with SQLSTATE 42P17 and names the relation that looped.CursorLovableBoltSupabasePostgresOct 27, 2026