Skip to content

How to delete a role in Postgres

摘自 Postgres 文档:

如果某个角色在集群的任何数据库中仍被引用,就无法删除它;如果尝试删除,会报错。在删除角色之前,你必须删除它拥有的所有对象(或重新分配这些对象的所有权),并撤销该角色在其他对象上被授予的任何权限。

首先确保 Postgres 拥有该角色的所有权:

🌐 First make sure that Postgres has ownership over the role:

1
GRANT <role> TO "postgres";

那么你必须重新分配该角色拥有的任何对象:

🌐 Then you must reassign any objects owned by role:

1
REASSIGN OWNED BY <role> TO postgres;

一旦所有权转移,你就可以运行以下查询:

🌐 Once ownership is transferred, you can run the following query:

1
DROP OWNED BY <role>;

DROP OWNED BY 会删除角色拥有的所有对象,理论上应该没有任何对象。但是,它也会撤销该角色的权限。一旦完成,你应该就可以运行:

1
DROP role <role>;

如果你遇到任何问题,请创建一个支持工单

🌐 If you encounter any issues, create a support ticket