Sign-up database trigger to insert into public users table #306
-
Hi, I'm trying to set up a trigger to automatically copy new users from CREATE OR REPLACE FUNCTION auth.signup_copy_to_users_table()
RETURNS TRIGGER LANGUAGE plpgsql AS $function$
BEGIN
INSERT INTO public.users(id)
VALUES(NEW.id);
RETURN NEW;
END;
$function$;
DROP TRIGGER IF EXISTS signup_copy on auth.users;
CREATE TRIGGER signup_copy
AFTER INSERT OR UPDATE ON auth.users
FOR EACH ROW EXECUTE PROCEDURE signup_copy_to_users_table(); In general it'd be useful to be able to keep the auth/public users tables in sync with the |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 57 replies
-
Same issue here. |
Beta Was this translation helpful? Give feedback.
-
I suspect it's permissions - the trigger is probably getting called by the Here is a snippet that should work: /**
* This trigger automatically creates a user entry when a new user signs up via Supabase Auth.
*/
create function public.handle_new_user()
returns trigger as $$
begin
insert into public.users (id)
values (new.id);
return new;
end;
$$ language plpgsql security definer;
create trigger on_auth_user_created
after insert on auth.users
for each row execute procedure public.handle_new_user(); |
Beta Was this translation helpful? Give feedback.
-
Wow, thanks @kiwicopple - this works 👍 Would this be a useful thing to include in the docs? |
Beta Was this translation helpful? Give feedback.
-
I am unable to work out what to do in terms of overcoming the fact that I am successfully adding email first name last name JWT and password into the auth.users table but try as I might syntax errors prevent me from finishing a function, I don't even know how to set up a function that observes changes from the auth table. I need more than help I need Jesus to take the wheel. |
Beta Was this translation helpful? Give feedback.
-
Hey Boss another question: Raw meta data apparently isnt recognising the following public trigger: This is the function as its written And heres the policies for that public.users table. And finally this is the sql editor query that created the public handler Why is it still not creating a record upon the user being added to auth.users |
Beta Was this translation helpful? Give feedback.
-
Here is how I did it using supabase triggers - https://stackoverflow.com/a/78582155/4350622 |
Beta Was this translation helpful? Give feedback.
I suspect it's permissions - the trigger is probably getting called by the
anon
user. You can try to addsecurity definer
to the function, which will use the same permissions as the user who created the function.Here is a snippet that should work: