For builders shipping this week
"new row violates row-level security policy" for profiles right after signup
Your own account already has a profile row, so the app looks fine. A stranger clicks Sign up and the form shows new row violates row-level security policy for table "profiles". FirstUser opens your live URL like a brand-new visitor, signs up, and tells you which step failed, with a screenshot and a fix prompt for Lovable, Bolt or Cursor. Run a free signup test.
Postgres reports that check as SQLSTATE 42501. The Supabase client puts 42501 on error.code and the sentence above on error.message. The auth user was created. The insert into public.profiles was not.
A missing table grant is also 42501, but the message is permission denied for table profiles. That is a GRANT, not a policy. The "new row violates" sentence means Row Level Security rejected the row.
Why the insert runs as anon
Generated Lovable, Bolt and Cursor apps often do this right after signup:
const { data, error } = await supabase.auth.signUp({ email, password })
if (error) throw error
await supabase.from('profiles').insert({
id: data.user.id,
first_name: firstName,
})The Dart signUp reference says that with Confirm email enabled, signUp() returns a user and session is null. With Confirm email disabled, it returns both. The JavaScript client returns that same auth response. On hosted projects, Confirm email is on by default, which is why this showed up after you shipped and not while you were clicking around already signed in.
data.user is still set, so if (data.user) does not save you. The next request uses the publishable (anon) key with no user JWT. Supabase's RLS guide says auth.uid() is null when the request has no signed-in user, and null = id is not true. A policy with check ((select auth.uid()) = id) rejects the row. That is what the policy is for.
The same toast appears for three other mistakes, and they can stack:
- No insert policy, or no
WITH CHECK. Afor selectpolicy does not allow inserts. With RLS on and no permissive insert policy for the role making the request, Postgres refuses the new row with this same message. An insert policy useswith check. If afor allpolicy sets onlyusing, Postgres uses that expression for the new row too. - The policy checks the wrong column. The user-management example stores the owner in
profiles.id. The RLS guide's sample table stores it inuser_id. A policy that comparesauth.uid()touser_idwhile the insert writesid(or the other way around) fails the check even after a session exists. - The insert runs before any session exists. Confirm email is the usual reason. The same error happens if the insert does not wait for
signUp(), or if it uses another client that never received the session. When Confirm email is off and this client did get the session,auth.uid()is the new user and a matching policy allows the row.
The safe fix: a trigger on auth.users
Create the profile inside the database, in the same moment Supabase inserts auth.users, and delete the insert from the browser. Supabase's user-management guide does this with a security definer function and set search_path = ''. A security definer function runs as the role that created it. Created in the SQL editor, that role is postgres, which bypasses RLS, so the insert does not depend on a session.
The snippet below is that documented trigger, with two changes you can re-run: create or replace and drop trigger if exists, and on conflict (id) do nothing. The conflict target needs id to be the primary key (the documented profiles table is). Edit the column list so it matches your table. Every not null column without a default needs a value.
create or replace function public.handle_new_user()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
begin
insert into public.profiles (id, first_name, last_name)
values (
new.id,
new.raw_user_meta_data ->> 'first_name',
new.raw_user_meta_data ->> 'last_name'
)
on conflict (id) do nothing;
return new;
end;
$$;
drop trigger if exists on_auth_user_created on auth.users;
create trigger on_auth_user_created
after insert on auth.users
for each row execute procedure public.handle_new_user();search_path = '' means every name that is not built in needs its schema. profiles without public. is not found, the trigger raises, and Auth rolls the signup back with Database error saving new user. That is a different error. See Database error saving new user.
If the owner column is user_id rather than id, insert user_id and write on conflict (user_id) only when that column has a unique constraint. Pass the fields as options.data on signUp(). The same guide stores options.data on raw_user_meta_data, which is what the trigger reads:
const { data, error } = await supabase.auth.signUp({
email,
password,
options: {
data: {
first_name: firstName,
last_name: lastName,
},
},
})
if (error) {
// show error.message
} else if (!data.session) {
// Confirm email is on. The trigger already inserted the profile.
// Show "Check your inbox". Do not insert into profiles.
}Remove the supabase.from('profiles').insert(...) that follows signUp(). Leaving it in place inserts a second time. After the trigger has created the row, that second insert is another 42501 while the session is null, or a duplicate-key error once a session exists.
Keep RLS enabled. The signed-in user still needs to read and update their own row, or the first screen is empty after they confirm. Match the column the table actually uses. For id:
alter table public.profiles enable row level security; create policy "Users can view their own profile." on public.profiles for select to authenticated using ( (select auth.uid()) = id ); create policy "Users can update their own profile." on public.profiles for update to authenticated using ( (select auth.uid()) = id ) with check ( (select auth.uid()) = id );
You do not need an insert policy for anon. The trigger does not use one. Swap id for user_id if that is the owner column.
People who signed up while this was broken have an auth.users row and no profile. Run this once in the SQL editor, not from the browser, and only with the columns you have:
insert into public.profiles (id, first_name, last_name)
select u.id,
u.raw_user_meta_data ->> 'first_name',
u.raw_user_meta_data ->> 'last_name'
from auth.users u
left join public.profiles p on p.id = u.id
where p.id is null
on conflict (id) do nothing;Do not "fix" it by opening the table
Do not run alter table public.profiles disable row level security. Supabase's RLS guide says a table in an exposed schema without RLS is readable and writable by any role with a grant on it. The publishable key is in your frontend. With RLS off, anyone who can load the app can read and write profiles.
Do not put the service_role key, or the secret key that replaces it, in the browser, in a VITE_ variable, or in a NEXT_PUBLIC_ variable. The same guide says the secret key bypasses RLS and must not be used in the browser. The service_role key is the legacy form of that key.
Do not add for insert to anon with check (true). That lets every signed-out visitor insert rows with the publishable key. It hides this toast and leaves the table open.
How this differs from "Database error saving new user"
Database error saving new user comes from Auth, before signUp() returns a user. A trigger on auth.users raised, Supabase rolled the new user back, and the docs warn that a failing signup trigger can block signups. The usual causes are a column the function still names, a not null column with no value, or a name left unqualified when search_path is empty.
This page is the other order. signUp() succeeded, the user exists, and the browser's own insert into profiles was rejected with 42501. Fixing the trigger does not remove a client insert that still runs, and deleting the client insert does not fix a trigger that raises.
After the client insert is gone, Confirm email still returns session: null. The app should show "Check your inbox" instead of redirecting to a page that requires a user. That screen is Email not confirmed right after signup.
Paste this into Lovable, Bolt or Cursor
New users see "new row violates row-level security policy" for the profiles table (Postgres 42501) right after sign-up. Do not disable RLS. Do not put the service_role key or the secret key in the browser, and do not add an insert policy for anon with check (true). Remove the profiles insert that runs in the client after supabase.auth.signUp(). Create the profile with a SECURITY DEFINER trigger on auth.users: set search_path = '', schema-qualify public.profiles, insert the owner column from new.id, copy name fields from new.raw_user_meta_data, and use on conflict do nothing on that column's primary key. Pass those fields through signUp options.data. After signUp, if data.session is null, show "Check your inbox" and do not redirect to a page that requires a user. Keep RLS on. Add select and update policies for authenticated that compare (select auth.uid()) to the real owner column, id or user_id.
Verify it as a real first user
Use a private window, the published URL, and an email address that has never signed up. Submit the form. The page should say to check email, not show 42501. In the Supabase table editor, auth.users and public.profiles should both have that person, with the same id, before they click the email. Then open the confirm link and check that the first screen loads their row. Your own account cannot show this, because its profile already exists.
Logs → Postgres at the time of the old attempt still shows the 42501 and the role that ran it. After the fix, a new attempt should not add another one.
Or let FirstUser be that stranger. It opens your live URL in a fresh browser and signs up with a new email. Run a free signup test.
FAQ
Can I fix this by turning off Row Level Security?
No. Supabase's Row Level Security guide says a table in an exposed schema without RLS is readable and writable by any role with a grant on it. Keep RLS on. Create the profile in the trigger instead of opening the table to anon.
Can I put the service_role key in the app so the insert works?
No. The service_role key, and the secret key that replaces it, bypass Row Level Security. Supabase says never to use a secret key in the browser. A VITE_ or NEXT_PUBLIC_ variable is bundled into the client. Use the publishable or anon key in the app, and create the profile in the trigger.
Is this the same as "Database error saving new user"?
No. "Database error saving new user" comes from Auth when a trigger on auth.users fails, and the new user is rolled back. This message (42501) comes back from the profiles insert after signUp already returned a user. See Database error saving new user for the trigger failure, and Email not confirmed right after signup for the screen to show when session is null.
Can FirstUser catch this?
FirstUser signs up on your live URL with a new email. It does not open your Supabase dashboard or read RLS policies. If your page shows the error text in a visible role="alert" or data-error element, the sign-up step is marked failed with "The sign-up form was rejected." and the diagnosis includes that text. If the page shows nothing, the step fails with "Sign-up was submitted, but the page did not show a success state." Either way you get a screenshot of the step. Run a free signup test.