Fix guides · Lovable, Bolt, Cursor ·
Fix "new row violates row-level security policy"
new row violates row-level security policy for table "profiles"
Supabase rejected a write to your profiles table because Row Level Security (RLS, per-row access rules) is on and no policy allows the current user to create that row. Usually the INSERT policy is missing, it checks the wrong column (profiles normally uses id, not user_id), or the insert runs before the user has a session. Fix the policy, or create profiles with a database trigger on signup. Don't turn RLS off.
Common causes – and how to tell which one you have
1. No INSERT policy on profiles
How to tell: The Policies page in the Supabase dashboard shows RLS enabled on profiles but no policy for INSERT or ALL. Reads may work while every insert fails.
Fix: Add an owner-only insert policy: create policy "Users can insert own profile" on public.profiles for insert to authenticated with check ((select auth.uid()) = id);
2. Policy checks the wrong column
How to tell: Your policy says auth.uid() = user_id, but your profiles table uses id as the user's id (it references auth.users). Or the insert never sets that column, so it ends up null or a default value.
Fix: Make the policy compare auth.uid() to the column that actually holds the user's id. Make the insert set that column to the signed-in user's id (from supabase.auth.getUser() or the session).
3. Insert runs before the user has a session
How to tell: It fails right after signUp(), especially when email confirmation is on. signUp then returns a user but no session, so the request runs as anon and auth.uid() is null.
Fix: Create the profile in the database instead. Add a security definer trigger on auth.users that inserts into public.profiles when a user signs up, as shown in the Supabase docs, then remove the client-side insert.
4. Upsert or insert-then-select without the matching policies
How to tell: Your code uses .upsert() or .insert(...).select(). A plain insert might work, but this call fails.
Fix: Upsert also needs an UPDATE policy (using and with check on auth.uid() = id). Returning the row with .select() needs a SELECT policy the user passes. Add owner-scoped policies for those commands.
5. Server code calls Supabase without the user's login
How to tell: The insert happens in an API route, server action or edge function that creates a client with the anon key but doesn't pass the user's cookies or Authorization header. The same insert works from the browser.
Fix: Create the server client with the user's session, for example with @supabase/ssr reading cookies, or by forwarding the Authorization header. The request then runs as authenticated and auth.uid() has a value.
6. Policy targets the wrong role or command
How to tell: A policy exists, but it is FOR SELECT or FOR UPDATE only, or it is limited TO anon while your users are logged in (authenticated).
Fix: Edit or replace the policy so it covers INSERT (or ALL) for the authenticated role, with a WITH CHECK expression that matches your row.
How to fix it
- Open your Supabase project, go to the Policies page and find the profiles table. Confirm RLS is enabled and note which policies exist and which column they compare to auth.uid().
- Check the table's columns. In most setups profiles.id is the user's id and references auth.users(id). Use that column in your policies, not a user_id column that may not exist.
- In the SQL Editor, add the policies: create policy "Insert own profile" on public.profiles for insert to authenticated with check ((select auth.uid()) = id); plus a matching SELECT policy (using) and an UPDATE policy (using and with check).
- If the insert happens right after signUp(), replace it with a trigger. Create a security definer function public.handle_new_user() that inserts new.id into public.profiles. Then add create trigger on_auth_user_created after insert on auth.users for each row execute procedure public.handle_new_user();
- Remove or adjust the client code so it doesn't insert a profile the trigger already created. For later edits, use .update() on the user's own row.
- Test while signed in as a normal user. Creating and editing your own profile should work, and reading or editing another user's profile should return nothing or an error.
Using Lovable, Bolt or v0?
In Lovable or Bolt, tell the tool: "Inserting into profiles fails with an RLS error. Add owner-only INSERT, SELECT and UPDATE policies using auth.uid() = id, and create profiles with a signup trigger instead of from the client. Don't disable RLS." Then open your Supabase dashboard and check the Policies page for profiles. You should see RLS enabled, plus policies that compare auth.uid() to the right column. Under Authentication settings, see whether email confirmation is on. If it is, client-side profile inserts right after signup will fail, and the trigger approach is the fix.
Using Cursor, Claude Code or Windsurf?
Paste the full error and ask the agent to read the profiles table schema and existing policies (from supabase/migrations or by querying pg_policies). Have it find every place the code writes to profiles before it changes anything. Ask for a SQL migration file rather than dashboard instructions, so the fix is versioned. Make it verify by signing in as a test user and running the insert, then confirming a second user can't read or overwrite that row.
The “fix” to avoid
Agents often 'fix' this by disabling RLS on profiles, adding a policy with check (true), or switching to the service_role key in frontend code. All three make the error disappear. They also let anyone with your public anon key read or edit every user's profile, and a service_role key in browser code gives full database access to anyone who opens DevTools. Keep RLS on. Use the service_role key only in trusted server code, and only when you really need to bypass RLS.
Copy-paste fix prompt for your AI coding tool
Paste this into Cursor, Claude Code, Lovable or Bolt:
Supabase returns "new row violates row-level security policy for table "profiles"" when we write to profiles. Before changing anything, inspect the profiles schema, its RLS policies, and every place the code inserts or upserts into it. Check in order: (1) is there an INSERT policy for authenticated; (2) does it compare auth.uid() to the column that holds the user id (usually id, not user_id); (3) does the insert run right after signUp with no session; (4) do upsert or .select() calls lack UPDATE or SELECT policies; (5) do server-side calls lack the user's session. Write a SQL migration with owner-only INSERT, SELECT and UPDATE policies. If profiles are created at signup, add a security definer trigger on auth.users instead of the client insert. Do not disable RLS, use check (true), or expose the service_role key. Verify as a signed-in test user, then confirm another user can't access that row.
Questions people ask
Why does creating a profile fail right after sign up?
If email confirmation is on, signUp() returns no session, so your insert runs as an anonymous user and auth.uid() is null. Create the profile with a database trigger on auth.users instead.
Is it safe to just disable RLS on the profiles table?
No. With RLS off, anyone with your public anon key can read and change every row through the API. Add correct policies instead.
Should I use auth.uid() = id or auth.uid() = user_id?
Use whichever column stores the user's id in your table. Most profiles tables use id as a foreign key to auth.users(id), so the check is auth.uid() = id.