Skip to content

Instantly share code, notes, and snippets.

@Sama-004
Created September 10, 2024 13:26
Show Gist options
  • Save Sama-004/78ef676029b16031e995c1e78c7dfcf4 to your computer and use it in GitHub Desktop.
Save Sama-004/78ef676029b16031e995c1e78c7dfcf4 to your computer and use it in GitHub Desktop.
-- inserts a row into public.user_profiles with only id and user_id
create or replace function public.handle_new_user()
returns trigger
language plpgsql
security definer set search_path = ''
as $$
begin
insert into public.user_profiles (id, user_id, username)
VALUES (new.id, new.id, (new.raw_user_meta_data->>'username'));
return new;
end;
$$;
-- trigger the function every time a user is created
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment