Supabase “Database error saving new user” – find the failing trigger and fix it

By Abheesht Tripathi · Published

AuthApiError: Database error saving new user   (HTTP 500, code: unexpected_failure)

What it means

Supabase Auth inserted a row into auth.users and the database rejected it. Auth has no idea why your schema said no, so it returns this generic message. In almost every project the real error comes from a trigger on auth.users, usually the handle_new_user() function that creates a row in public.profiles. Because the trigger runs in the same transaction, its failure rolls back the sign-up.

Step 1: read the real error

Open the Auth logs in the dashboard, then the Postgres logs around the same time. The Postgres line names the problem and the function. Typical lines:

relation "profiles" does not exist (SQLSTATE 42P01)
  -- context: PL/pgSQL function public.handle_new_user()
null value in column "username" of relation "profiles" violates not-null constraint (SQLSTATE 23502)
duplicate key value violates unique constraint "profiles_username_key" (SQLSTATE 23505)
permission denied for table profiles (SQLSTATE 42501)

What each one means

Step 2: a trigger that does not break sign-up

This follows Supabase's recommended shape: security definer, an empty search path, fully qualified names, and defaults for anything that might be missing:

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();

Don't make profile columns not null or unique unless every sign-up path really provides a value. Collect a username after sign-up instead, and enforce uniqueness there with a friendly error.

Step 3: lock the function down

A security definer function in public can be called through the API unless you revoke it. It's only meant for the trigger:

revoke execute on function public.handle_new_user() from public, anon, authenticated;

Other places this message comes from

Supabase lists the same family of errors for creating, updating and deleting users from the dashboard: Database error creating new user, Database error updating user and Database error deleting user. Triggers that fire on update (for example on last_sign_in_at) and foreign keys that block deletes are the usual causes. Tools that manage the whole database schema, such as some ORMs, can also change permissions on auth.users.

How to verify

Sign up with email and password, then with each OAuth provider you use. Each sign-up succeeds, and select * from public.profiles order by id desc limit 5; shows a row per user. The Postgres logs show no errors from handle_new_user.

FAQ

Can I make the profile insert optional so sign-up never fails?

Yes: wrap the insert in a BEGIN ... EXCEPTION WHEN others THEN ... END block inside the function and log the error. Sign-up then succeeds even if the profile insert fails, so you also need a way to create missing profiles later.

Related guides

More in Auth, JWT and secrets · all guides