DROP ROLE 是一个用于移除数据库角色的 DDL 命令。
DROP ROLE 于 PostgreSQL 8.1 中引入,取代了原有的 DROP USER 和 DROP GROUP 命令(这些命令被保留作为 DROP ROLE 的别名)。
用法
DROP ROLE 会从 PostgreSQL 实例中移除指定的角色。
在执行 DROP ROLE 之前,PostgreSQL 实例中由该角色拥有的任何对象都必须被重新分配或移除。
然而,无论是否有其他角色是待删除角色的成员,DROP ROLE 都会成功执行。
变更历史
- PostgreSQL 8.2
DROP ROLE IF EXISTS ...语法已添加(提交 f8b54fe6)
- PostgreSQL 8.1
- 添加(提交 7762619e)
示例
基本 DROP ROLE 执行
postgres=# DROP ROLE foo; DROP ROLE
尝试删除不存在的角色
postgres=# DROP ROLE foo; ERROR: role "foo" does not exist
安全地尝试删除可能不存在的角色
postgres=# DROP ROLE IF EXISTS foo; NOTICE: role "foo" does not exist, skipping DROP ROLE
尝试删除当前数据库中仍拥有一项或多项对象的角色
postgres=# DROP ROLE foo; ERROR: role "foo" cannot be dropped because some objects depend on it DETAIL: owner of database bar owner of table baz
尝试删除 PostgreSQL 实例中不同数据库里仍拥有一项或多项对象的角色
postgres=# DROP ROLE foo; ERROR: role "foo" cannot be dropped because some objects depend on it DETAIL: 1 object in database bar
参考资料
- PostgreSQL 文档: DROP ROLE
有用链接
- 如何在 PostgreSQL 中 DROP ROLE 或 DROP USER - Laurenz Albe / CyberTec 于 2022 年 3 月的文章
