语法
drop role — remove a database role
drop role [ if exists ] name [, ...]
描述
drop role
removes the specified role(s). to drop a superuser role, you must be a superuser yourself; to drop non-superuser roles, you must have createrole
privilege.
postgres=# create user u1 with superuser;
create role
postgres=# create user u2;
create role
postgres=# create user u3;
create role
postgres=# \du
list of roles
role name | attributes | member of
----------- ------------------------------------------------------------ -----------
lxs | superuser, create role, create db | {}
postgres | superuser, create role, create db, replication, bypass rls | {}
u1 | superuser | {}
u2 | | {}
u3 | | {}
postgres=# \c - u2
you are now connected to database "postgres" as user "u2".
postgres=> drop user u1;
error: permission denied to drop role
postgres=> drop user u3;
error: permission denied to drop role
postgres=# alter user u2 createrole;
alter role
postgres=# drop user u3;
drop role
postgres=# alter user u2 superuser;
alter role
postgres=# drop user u1;
drop role
postgres=# \c - postgres
you are now connected to database "postgres" as user "postgres".
postgres=# drop user u2;
drop role
postgres=#
a role cannot be removed if it is still referenced in any database of the cluster; an error will be raised if so.
postgres=# create user u1;
create role
postgres=# create table t1(id int);
create table
postgres=# grant select on t1 to u1;
grant
postgres=# drop user u1;
error: role "u1" cannot be dropped because some objects depend on it
detail: privileges for table t1
postgres=#
before dropping the role, you must drop all the objects it owns (or reassign their ownership) and revoke any privileges the role has been granted on other objects. the reassign owned and drop owned commands can be useful for this purpose;
postgres=# \du
list of roles
role name | attributes | member of
----------- ------------------------------------------------------------ -----------
lxs | superuser, create role, create db | {}
postgres | superuser, create role, create db, replication, bypass rls | {}
u1 | | {}
postgres=# reassign owned by u1 to lxs;
reassign owned
postgres=# reassign owned by u1 to lxs;
reassign owned
postgres=# drop owned by u1;
drop owned
postgres=# drop user u1;
drop role
postgres=#
however, it is not necessary to remove role memberships involving the role; drop role automatically revokes any memberships of the target role in other roles, and of other roles in the target role. the other roles are not dropped nor otherwise affected.
参数
-
if exists
do not throw an error if the role does not exist. a notice is issued in this case.
postgres=# drop user if; error: role "if" does not exist postgres=# drop user if exists u1; notice: role "u1" does not exist, skipping drop role postgres=#
-
name
the name of the role to remove.
postgres=# create user u1; create role postgres=# drop user u1; drop role postgres=#
注解
postgresql includes a program dropuser
that has the same functionality as this command (in fact, it calls this command) but can be run from the command shell.
postgres=# create user u1;
create role
postgres=# \du
list of roles
role name | attributes | member of
----------- ------------------------------------------------------------ -----------
lxs | superuser, create role, create db | {}
postgres | superuser, create role, create db, replication, bypass rls | {}
u1 | | {}
postgres=# \q
[postgres@lyp ~]$ dropuser u1;
[postgres@lyp ~]$ psql
psql (14.1)
type "help" for help.
postgres=# \du
list of roles
role name | attributes | member of
----------- ------------------------------------------------------------ -----------
lxs | superuser, create role, create db | {}
postgres | superuser, create role, create db, replication, bypass rls | {}
postgres=#
兼容性
the sql standard defines drop role
, but it allows only one role to be dropped at a time, and it specifies different privilege requirements than postgresql uses.
sql 标准定义了drop role
, 但是它只允许一次删除一个角色并且它指定了和 postgresql不同的特权需求。