Fix guides · Lovable, Bolt, Cursor, Supabase ·
Fix Supabase RLS "new row violates row-level security policy" (42501)
new row violates row-level security policy for table "profiles"
Supabase blocks writes to the profiles table when Row Level Security is enabled and no INSERT policy allows the current user to create rows. The typical fix is an owner-only INSERT policy using auth.uid() = id.
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 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 wrong column
How to tell: If the INSERT policy references a column that doesn't exist in the table schema, CREATE POLICY fails when created. A missing INSERT policy causes the runtime 42501 error on insert.
Fix: Compare auth.uid() to the column that actually holds the user's id (usually id, not user_id). Make sure the INSERT sets that column to the signed-in user's id.
3. Insert runs before user has a session
How to tell: It fails right after signUp(), especially with email confirmation on. signUp returns a user but no session, so the request runs as anon and auth.uid() is null.
Fix: Create a profile with a database trigger on auth.users instead of client-side insert. Add security definer function public.handle_new_user() and trigger on_auth_user_created.
How to fix it
- Open Supabase dashboard → Database → Policies → profiles table. Confirm RLS is enabled and inspect existing policies.
- Check table schema: profiles.id should reference auth.users(id). INSERT policy commonly compares auth.uid() to id, not user_id. Many apps use a user_id column with a separate id primary key.
- If email confirmation is on: client-side inserts after signUp() fail due to no session. Use a security definer trigger instead.
- Create security definer function: CREATE FUNCTION public.handle_new_user() RETURNS trigger AS $$ BEGIN INSERT INTO public.profiles (id) VALUES (NEW.id); RETURN NEW; END $$ LANGUAGE plpgsql SECURITY DEFINER SET search_path = public;
- Add trigger: CREATE TRIGGER on_auth_user_created AFTER INSERT ON auth.users FOR EACH ROW EXECUTE FUNCTION public.handle_new_user();
- Test as a signed-in user: creating and editing own profile should work; another user cannot read or overwrite that row.
- Remove any client-side insert into profiles if using the trigger — both running would cause a duplicate primary key error.
Using Lovable, Bolt or v0?
In Supabase dashboard: enable RLS on profiles, then add policies using auth.uid() = id. For signup triggers: create the security definer function and trigger in the SQL Editor. Test as a signed-in user. Never disable RLS or use check (true).
Using Cursor, Claude Code or Windsurf?
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. Ask for a SQL migration file rather than dashboard instructions. Verify by signing in as a test user and running the insert, then confirming a second user can't access that row.
The “fix” to avoid
Disabling RLS on profiles lets anyone with the anon key read/write every row. Using user_id instead of id breaks the check. Adding a policy with check(true) or switching to service_role key gives full database access to anyone who opens DevTools. Keep RLS on. Use the service_role key only in trusted server code.
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'. First check: is RLS enabled on profiles? Do INSERT, SELECT and UPDATE policies exist? Does the INSERT policy compare auth.uid() to the id column (not user_id)? If email confirmation is on, client-side inserts right after signUp() will fail — use a database trigger instead. Write a SQL migration with owner-only INSERT, SELECT and UPDATE policies. Test as a signed-in test user, then confirm another user can't access that row.