简体   繁体   English

在 Postgres 中创建新角色

[英]Creating new Role in Postgres

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.我想在 PostgresQL 中编写一个 IF 条件,它将在数据库中创建一个新角色,但应该以这样的方式工作。

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.我首先检查一个 IF 语句,如果现有用户具有创建新角色的权限,并且如果用户只有该权限,则执行 CREATE ROLE 语句。

Something like就像是

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

Can this be done?这可以做到吗?

I would simply try an run the create role statement and let it fail, notifying whoever runs the script that something went wrong.我会简单地尝试运行create role语句并让它失败,通知运行脚本的人出现问题。

However, you can do what you want by checking if the current user has the createrole privilege:但是,您可以通过检查当前用户是否具有createrole权限来做您想做的事情:

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

     create role rolename;
  end if;
end;
$$
;  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM