Supabase signUp fails with "Database error saving new user": fix the auth.users trigger
By AI App Medic · Updated · Supabase · Auth · Lovable · Triggers
Your signup form worked last week. Now every new user gets an error and nobody new appears in Authentication, Users. The response from Supabase looks like this:
AuthApiError: Database error saving new user
status: 500
code: "unexpected_failure"
With Google or another OAuth provider, the user comes back to your app with something like ?error=server_error&error_description=Database+error+saving+new+user in the URL. Inviting a user from the dashboard fails with a similar database error. All of these point to the same place: something in your database refused the new row in auth.users.
What is actually failing
Supabase Auth does not know about your profiles table. When someone signs up, it inserts one row into auth.users. Most Lovable, Bolt and Cursor projects also have a trigger on that table that copies the new user into public.profiles. The trigger runs inside the same transaction. If it throws for any reason, Postgres rolls back everything, the user is not created, and Auth reports the generic message above.
So the real question is never "why is Auth broken". It is "what error did my trigger throw". The usual culprits, in rough order of how often we see them:
- The function is
security invoker(the default), so it runs assupabase_auth_admin, which has no rights onpublic.profiles. - The function sets an empty
search_pathbut refers toprofilesinstead ofpublic.profiles. - A column was renamed or dropped by a later migration, and the function still writes to it.
- A
NOT NULLcolumn is filled from metadata that this signup path does not send. - A
uniqueusername is generated from the email prefix, and two people share one. - The metadata is cast to an enum or a number and a value does not fit.
Step 1: get the real error from the logs
Reproduce the failed signup, then open the Logs section of the Supabase dashboard. Start with the Auth logs and find the 500 for /signup or /callback. The message usually includes the Postgres error. Then check the Postgres logs at the same timestamp for the full text, for example:
ERROR: permission denied for table profiles (SQLSTATE 42501)
ERROR: relation "profiles" does not exist (SQLSTATE 42P01)
ERROR: column "username" of relation "profiles" does not exist (SQLSTATE 42703)
ERROR: null value in column "full_name" of relation "profiles" violates not-null constraint (SQLSTATE 23502)
ERROR: duplicate key value violates unique constraint "profiles_username_key" (SQLSTATE 23505)
context: PL/pgSQL function public.handle_new_user() line 3 at SQL statement
The SQLSTATE tells you which of the causes above you have. Do not skip this step and start rewriting things blind. AI tools that "fix" this without reading the log tend to add a second trigger, which then fails too.
Step 2: list the triggers on auth.users
select t.tgname as trigger_name,
p.proname as function_name,
p.prosecdef as is_security_definer,
p.proconfig as settings
from pg_trigger t
join pg_proc p on p.oid = t.tgfoid
where t.tgrelid = 'auth.users'::regclass
and not t.tgisinternal;
You want one trigger per job. If you see on_auth_user_created and handle_new_user_trigger both inserting into profiles, one of them will hit a duplicate key on every signup. Drop the extra one. If is_security_definer is false, that alone explains a 42501. To read the current function body:
select pg_get_functiondef('public.handle_new_user'::regproc);
Step 3: replace the function with a safe version
This is the pattern from the Supabase docs, with a few additions that handle the missing-data cases:
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, avatar_url)
values (
new.id,
coalesce(
new.raw_user_meta_data ->> 'full_name',
new.raw_user_meta_data ->> 'name',
''
),
new.raw_user_meta_data ->> 'avatar_url'
)
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();
What each part does:
security definerruns the function as its owner (postgreswhen you create it in the SQL Editor), so the Auth role's limited rights no longer matter.set search_path = ''stops anyone from shadowing your tables with objects in another schema. It also means every table must be written aspublic.profiles, never justprofiles.coalescecovers the fact that email signup and Google send different metadata keys. Google typically sendsfull_name,nameandavatar_url; your email form sends whatever you passed inoptions.data.on conflict (id) do nothingmeans a retry, a duplicate trigger, or a profile your app already created cannot block signup.
Match the column list to your real table. If the log showed a column error, either add the column or remove it from the insert. If a column is NOT NULL but not always known at signup, give it a default or make it nullable. Generated usernames should be left out of the trigger: let the user pick one after signup, or append part of the user id so two people named alex cannot collide.
Step 4: test every way a user can be created
- Sign up with a fresh email address and password.
- Sign in with each OAuth provider you offer, using an account that has never signed in before.
- Invite a user from Authentication, Users in the dashboard.
- Check that each one has a row:
select id, full_name from public.profiles order by id desc limit 5;
If signup now succeeds but the confirmation email never arrives, that is a separate problem, covered in Supabase auth email not sending. If Google stops at a Google error page before returning to your app, see redirect_uri_mismatch with Supabase.
Other causes that give the same message
- A constraint on auth.users. Someone added a check or a foreign key to the table Supabase manages. Drop it and enforce the rule in your own tables.
- An update trigger. "Database error updating user" or an error about the
last_sign_infield on login comes from a trigger that fires on update. Apply the same security definer fix to it. - Users inserted with raw SQL. If an AI tool seeded users with a direct
insert into auth.users, logins can fail with "Database error querying schema" and a NULL token column. Set that column to an empty string where it is null and create users through the Auth API from then on. - Prisma or another ORM changed permissions on the
authschema while running migrations. Keep ORM migrations out ofauth.
Prevention
Treat the signup trigger as production code. Keep it in a migration, not only in the dashboard. Every time a migration renames or adds a required column on profiles, update the trigger in the same migration. When you add a new sign-in method, test a brand new account with it before you ship. And when you ask an AI tool to change the profiles table, ask it to show you the current handle_new_user definition first, so it does not break signup for everyone after you.
FAQ
Does this error mean Supabase Auth is down?
Almost never. It is a generic wrapper Supabase Auth returns when the INSERT into auth.users fails. The usual reason is your own trigger on auth.users throwing an error, which rolls back the whole signup, so the user is not created either.
Why does it only fail for Google sign-in?
Email signup and OAuth send different metadata. Google provides full_name, name and avatar_url, while your email form may send something else, or the reverse. If the trigger reads a key that is missing and the column is NOT NULL, one path works and the other fails. Use coalesce and defaults.
Should I catch every error in the trigger so signup never fails?
You can, with an exception block that raises a warning and returns new, but then a user can exist without a profile row. If you do it, make the app create the missing profile on first login. Fixing the actual error is better.
I see Database error querying schema when users log in. Is that the same thing?
It is related. That one usually appears when users were inserted straight into auth.users with SQL, often by an AI tool, leaving token columns NULL. Supabase's fix is to set the column named in the error to an empty string where it is null, and to create users through the Auth API from then on.
Related guides
- Supabase auth email not sending in a Lovable app: the SMTP setup that actually works
- Google Error 400: redirect_uri_mismatch with Supabase: the exact URI to add
- Supabase "permission denied for table" on a new table: add the missing GRANT
- Supabase "email rate limit exceeded" (429): why signup breaks after two emails and how to fix it
- Supabase PGRST116 "JSON object requested, multiple (or no) rows returned": what .single() is telling you