Supabase: "Database error saving new user" on sign-up

AuthApiError: Database error saving new user (HTTP 500, code "unexpected_failure"). Related: "Database error creating new user" when inviting or adding a user in the dashboard.

What you see

Your Lovable, Bolt, Cursor or v0 app has a sign-up form backed by Supabase Auth. Log-in for existing users still works, but every new sign-up fails. The browser console or network tab shows:

AuthApiError: Database error saving new user

The response is an HTTP 500 with "code": "unexpected_failure". Inviting or adding a user from the Supabase dashboard fails too, often as “Failed to create user: Database error creating new user”. Supabase’s troubleshooting page on managing users lists both messages under the same cause.

The message itself is generic on purpose. The useful part, the actual Postgres error, is in your logs.

Why it happens

Creating a user inserts a row into auth.users. Anything attached to that insert runs in the same transaction, so if it fails, the sign-up fails. Supabase’s troubleshooting page names the usual culprits:

  • a trigger or trigger function on auth.users that has an error,
  • a constraint on auth.users that isn’t being met,
  • Prisma having broken the permissions on auth.users.

In AI-built apps it’s nearly always the first one. The classic pattern is a handle_new_user() function that copies each new user into a public.profiles table. Supabase’s own User Management docs warn that “if the trigger fails, it could block signups”. It breaks in a few predictable ways:

  1. The table it writes to doesn’t exist. The Auth log shows relation "profiles" does not exist (SQLSTATE 42P01) and the Postgres log points at PL/pgSQL function public.handle_new_user() line 3. The table was never created, was dropped, or the AI renamed it.
  2. A column it writes no longer exists. column "updated_at" of relation "profiles" does not exist (SQLSTATE 42703). In issue #3292 the cause was a removed column that the trigger function still inserted. That’s exactly what happens when you ask your AI builder to “simplify the profiles table” and it doesn’t touch the trigger.
  3. Permission denied. permission denied for table profiles (SQLSTATE 42501). The role that runs Auth, supabase_auth_admin, only has the permissions it needs for the auth schema. Supabase’s fix is to make the function security definer, so it runs with the privileges of the role that created it (postgres).
  4. An unqualified table name with an empty search path. The recommended function sets search_path = ''. The docs spell it out: Postgres then “can’t find a name like profiles without its schema”, the trigger fails, “and every sign-up fails with Database error saving new user.”
  5. The function doesn’t return. In discussion #36215 a magic-link sign-up returned unexpected_failure / “Database error saving new user”, and the fix was the missing return new; statement.

A related variant: if users were inserted into auth.users with raw SQL (some AI tools do this to “seed” accounts), Supabase notes that required columns can end up NULL and log-in then fails with 500: Database error querying schema. The fix for that is on the same page. Create users through the Auth API instead.

Fix it

1. Read the real error

Try one sign-up and note the time. In the Supabase dashboard open Logs, then the Auth logs, and find the entry at that time. Then check the Postgres logs for the same moment. You’re looking for one of the SQLSTATE lines above. Don’t change anything until you know which one it is.

2. Find the trigger and its function

In the dashboard, Database → Triggers shows triggers on the auth schema (usually on_auth_user_created), and Database → Functions shows the function it calls. Or run this in the SQL editor:

select tgname, tgfoid::regproc as function_name
from pg_trigger
where tgrelid = 'auth.users'::regclass and not tgisinternal;

3. Recreate the function the way Supabase documents it

Make it match your current table. Every column it inserts must exist, and any not null column without a default must get a value. This is the pattern from Supabase’s User Management docs, adapted to a typical profile:

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, new.raw_user_meta_data ->> 'full_name');
  return new;
end;
$$;

Three things matter here: security definer (fixes 42501), public.profiles with the schema written out (fixes the empty search path), and return new;. If the table is missing, create it first. The docs’ example references auth.users with on delete cascade and enables Row Level Security on it.

If an old, broken version keeps coming back, Supabase’s troubleshooting page drops the function and its trigger together with drop function public.handle_new_user() cascade;. Recreate both afterwards:

create trigger on_auth_user_created
  after insert on auth.users
  for each row execute procedure public.handle_new_user();

4. Stop it from happening again

The trigger and the table drift apart whenever your AI builder edits one and forgets the other. A prompt that helps: “Sign-up fails with ‘Database error saving new user’. Show me the function called by the trigger on auth.users and the current columns of public.profiles. Make the function insert only columns that exist, use security definer with set search_path = ” and fully qualified names like public.profiles, and end with return new. Don’t change the auth schema.”

Check it worked

  1. Sign up with an email you haven’t used before. No 500, and the user appears under Authentication → Users.
  2. Look in public.profiles: there’s a row whose id matches the new user’s id.
  3. Invite a user from the dashboard. It should succeed too, since it goes through the same trigger.
  4. Make sure the profiles table still has Row Level Security enabled. security definer lets the trigger write, but your users should only read and edit their own row.

A broken sign-up is invisible until a real user hits it. Run the free production-readiness checker on your live URL to catch exposed keys, missing RLS and leftover localhost redirects. If you want someone to go through your whole auth setup (triggers, RLS policies, redirects, sessions) before launch, get a written pre-launch audit.