0

I want to write an IF Condition in PostgresQL which will CREATE a new ROLE in the DB, but should work in such a way that.

I first check in an IF statement if the existing user has the privilege to CREATE a new ROLE and if the user has the privilege only then execute the CREATE ROLE statement.

Something like

IF ( Current user has privilege to create new ROLE ) THEN
   CREATE ROLE rolename;
end if;

Can this be done?

1 Answer 1

2

I would simply try an run the create role statement and let it fail, notifying whoever runs the script that something went wrong.

However, you can do what you want by checking if the current user has the createrole privilege:

do
$$
begin
  if (select rolcreaterole 
      from pg_roles
      where rolname = current_user) then 

     create role rolename;
  end if;
end;
$$
;  
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you @a_horse_with_no_name

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.