Supabase "Database error saving new user": the real cause and the safe fix
Your sign-up form fails with AuthApiError: Database error saving new user (status 500), and no user is created. Supabase Auth itself is almost never the problem. In nearly every case a trigger on auth.users, usually called handle_new_user, tries to insert a row into your profiles table and that insert fails. The error message hides the real reason, so the first step is to find it.
Step 1: find the real error
In the Supabase dashboard open Logs → Postgres (or Auth logs) and look for an ERROR at the moment of the failed sign-up. You'll see one of the messages below. Then list the triggers on auth.users:
select tgname, tgfoid::regproc as function
from pg_trigger
where tgrelid = 'auth.users'::regclass and not tgisinternal;
The usual causes
1. permission denied for table profiles
The trigger runs as the Auth service's own role, which isn't allowed to write to your public tables. The function needs to run as its owner instead, with security definer. This is the most common cause, especially when an AI tool wrote the trigger.
2. relation "profiles" does not exist
The function says profiles instead of public.profiles, and the Auth role's search path doesn't include public. Always use the full name.
3. null value in column ... violates not-null constraint
The profile table has a required column (username, full_name) and the trigger fills it from raw_user_meta_data, but the sign-up didn't send that field. Either send it in options.data on signUp, give the column a default, or use coalesce(new.raw_user_meta_data->>'full_name', '').
4. duplicate key value violates unique constraint
A unique username taken from metadata clashes with an existing one, or a profile row was left behind after a user was deleted. Use on conflict (id) do nothing for the id, and make usernames unique in your app before sign-up.
5. Type mismatch
profiles.id is bigint or text instead of uuid. It should be uuid and reference auth.users(id).
The safe trigger
create or replace function public.handle_new_user()
returns trigger
language plpgsql
security definer set search_path = ''
as $$
begin
insert into public.profiles (id, full_name)
values (new.id, coalesce(new.raw_user_meta_data->>'full_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 function public.handle_new_user();
security definer lets the function write to profiles; set search_path = '' stops anyone from tricking it into using a different table, which is why every name inside is written as public.….
What not to do
- Don't disable RLS on
profilesto make sign-up work. The trigger doesn't need it, and your users' profiles become readable and editable by anyone with your public anon key. - Don't
grant allonprofilestoanon. Same problem. - Don't create profiles from the browser with the service_role key. It bypasses every policy for everyone who opens your site. If that already happened, see what to do when the service_role key is exposed.
Once sign-up works, users still need policies to read and update their own profile. If those are missing, reads come back empty: see why Supabase returns an empty array, or build them with the RLS policy generator.
A prompt to paste into Lovable, Bolt or Cursor
Sign-up fails with "Database error saving new user". Fix it safely:
1. Show me every trigger on auth.users and the function it calls.
2. Rewrite the function as SECURITY DEFINER with set search_path = '',
use full names (public.profiles), coalesce() any value read from
raw_user_meta_data, and add "on conflict (id) do nothing".
3. Make sure profiles.id is uuid and references auth.users(id).
Keep RLS enabled on profiles. Do not grant anything to anon, do not
disable RLS, and do not use the service_role key in the frontend.
After launch, LeakPatrol's free scan checks your live app for leaked keys and other common mistakes in about a minute.
Check your app in a minute
Free, read-only, no sign-up. Every problem comes with a plain-English fix and a prompt for your AI tool.
Get the 10-point checklist
Every check we run on AI-built apps, with how to do each one yourself. Straight to your inbox.
Plus the odd security tip. Unsubscribe any time.
Questions
What does "Database error saving new user" mean in Supabase?
Supabase Auth could not save the new user because something in the database failed during the insert, almost always a trigger on auth.users that creates a row in a profiles table. The Postgres logs show the real error.
How do I fix the handle_new_user trigger?
Make the function SECURITY DEFINER with set search_path = empty, refer to tables by full name such as public.profiles, use coalesce for values from raw_user_meta_data, and add on conflict (id) do nothing.
Should I disable RLS on the profiles table to fix sign-up?
No. A SECURITY DEFINER trigger does not need RLS disabled, and turning it off lets anyone with your public anon key read and change every profile.
Where do I see the real error behind it?
In the Supabase dashboard under Logs, Postgres (or Auth). Look for the ERROR line at the time of the failed sign-up, for example permission denied, relation does not exist, or a not-null violation.
More guides
- Supabase RLS policy generator (free)
- Is this API key safe in my frontend? Free key checker
- Security scan MCP server for Claude Code, Cursor and other AI agents
- Supabase's October 30 change: what it fixes for your app's security, and what it doesn't
- Supabase "42501 permission denied for table": the safe fix (not GRANT ALL)
- Supabase "new row violates row-level security policy": 5 causes and the safe fix
- Supabase service_role key exposed? What to do in the next hour
- Supabase select returns an empty array but the data is there: the RLS fix
- Supabase Storage upload: "new row violates row-level security policy" fixed
- Supabase "infinite recursion detected in policy for relation" fixed
- Supabase Edge Function "blocked by CORS policy" fixed
- Supabase "Forbidden use of secret API key in browser" (401) fixed
- Supabase "Invalid API key" and "Legacy API keys are disabled" fixed
- Supabase login or magic link redirects to localhost:3000 (fixed)
- Is your Supabase storage bucket public? What anyone can see, and the fix
- Lovable data exposure study: is your app one of the 24,664?
- Lovable security check: is your Lovable app safe?
- Bolt.new security check: is your Bolt app safe?
- Launch checklist for apps built with Lovable, Bolt and Cursor
- Supabase security check: is your anon key safe? (RLS checker)
- How to check if your API key is exposed on your website
- Are VITE_ env variables secret? How to hide API keys
- Stop users giving themselves your paid plan (Supabase)