5

Is there some way to create a user/role/group in PostgreSQL via SQL only if it does not exist yet? The only solution I have found so far is creating a custom stored procedure, since that supports conditionals. Is there a simpler solution that I have overlooked? Thank you!

1 Answer 1

8

You can use a DO block to only create the role if it doesn't exist, no need for a procedure:

do
$$
begin
  if not exists (select * from pg_user where usename = 'new_role') then
     create role new_role password '...';
  end if;
end
$$
;
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.