為什麼無法在 RDS for PostgreSQL 資料庫執行個體中刪除使用者或角色?
當我嘗試在 Amazon Relational Database Service (Amazon RDS) 執行個體中刪除使用者或角色時,收到錯誤 "role cannot be dropped because some objects depend on it" (因為某些物件與之相依,無法刪除角色)。
簡短描述
當 RDS for PostgreSQL 中的使用者或角色建立物件 (如資料表或結構描述) 時,使用者或角色即所建立物件的擁有者。如果您嘗試刪除擁有任何資料庫中一個或多個物件的使用者或角色,或擁有這些物件的權限,則會收到錯誤訊息,指示有物件與該使用者或角色,以及授與的權限相依 (若有)。
若要刪除具有相依物件的使用者或角色,您必須執行下列操作:
- 將這些物件的擁有權重新指派給其他使用者。
- 撤銷授予使用者或角色的任何權限。
**注意:**如果不再需要這些物件,請考慮刪除這些物件,然後刪除角色。您可以使用 DROP OWNED 命令,刪除資料庫中某個角色擁有的所有物件。此外,您還可以撤銷授予該資料庫或共用物件中物件角色的任何權限。成功執行 DROP OWNED 命令之後,即可刪除該角色。
解決方案
在下列範例中,會使用三種不同的資料庫角色:
- test_user:這是必須刪除的使用者或角色。
- admin_user:這是用於刪除所需使用者或角色的角色。此使用者是 RDS 中最高權限的使用者,並連接 rds_superuser 角色。
- another_user:這是被指派 test_user 所擁有物件擁有權的使用者或角色。
執行下列命令以查看您登入時使用的角色:
pg_example=> SELECT current_user;
輸出類似於以下內容:
current_user -------------- admin_user (1 row)
當您嘗試刪除具有相依物件的使用者或角色時,收到類似下列內容的錯誤:
pg_example=> DROP ROLE test_user; ERROR: role "test_user" cannot be dropped because some objects depend on it DETAIL: privileges for database pg_example owner of table test_table owner of schema test_schema owner of sequence test_schema.test_seq privileges for table test_t2
在此範例中,被刪除的角色是 test_user。請注意,目前登入的角色是 admin_user,這是資料庫的主要使用者。
從錯誤訊息中,您會取得下列資訊:
- 角色 test_user 具有在資料庫 pg_example 和資料表 test_t2 上授予的權限。
- 角色 test_user 擁有資料表 test_table、結構描述 test_schema,以及 test_schema 中的序列物件 test_seq。
**注意:**如果您在連線至其他資料庫時刪除使用者或角色,則會收到類似下列內容的輸出:
pg_another_db=> DROP ROLE test_user; ERROR: role "test_user" cannot be dropped because some objects depend on it DETAIL: privileges for database pg_example 4 objects in database pg_example
若要查看使用者或角色擁有的物件,請務必連線至擁有物件所在的資料庫。
若要刪除使用者或角色,必須將擁有物件的擁有權重新指派給其他使用者或角色,並撤銷關聯的權限。您可以使用 PostgreSQL REASSIGN OWNED 命令,將這些物件的擁有權重新指派給其他使用者。執行此命令時,您可能會收到類似下列內容的錯誤:
pg_example=> select current_user; current_user -------------- test_user pg_example=> REASSIGN OWNED BY test_user TO another_user; ERROR: permission denied to reassign objects
若要解決此問題,必須將使用者或角色授予要重新指派擁有權的使用者。您不能使用 test_user 執行此操作,因為 test_user 不是 another_user 的擁有者。因此,您可能會看到類似下列內容的錯誤:
pg_example=> select current_user; current_user -------------- test_user pg_example=> grant another_user to test_user; ERROR: must have admin option on role "another_user"
您可以執行下列任一操作,將使用者或角色授予要重新指派擁有權的使用者:
- 登入您的主要使用者並執行 GRANT 命令:
pg_example=> select current_user; current_user -------------- admin_user pg_example=> GRANT another_user TO test_user; GRANT ROLE
- 登入要重新指派擁有權的使用者,並執行 GRANT 命令:
pg_example=> select current_user; current_user -------------- another_user pg_example=> GRANT another_user TO test_user; GRANT ROLE
選擇上述其中一個選項,並登入 test_user 之後,將 test_user 擁有的物件擁有權重新指派給 another_user︰
pg_example=> select current_user; current_user -------------- test_user pg_example=> reassign owned by test_user to another_user; REASSIGN OWNED
如果您登入主要使用者並嘗試刪除仍擁有現有權限的 test_user,則可能會收到類似下列內容的錯誤:
pg_example=> select current_user; current_user -------------- admin_user pg_example=> DROP ROLE test_user; ERROR: role "test_user" cannot be dropped because some objects depend on it DETAIL: privileges for database pg_example privileges for table test_t2
在此情況下,即使 RESSIGN 命令成功執行,您也會收到錯誤。這是因為,必須撤銷 test_user 的權限。執行 REVOKE 命令,從 test_user 擁有權限的任何物件撤銷所有使用權限。在此範例中,撤銷資料庫 pg_example 和資料表 test_t2 上 test_user 的權限。
pg_example=> REVOKE ALL ON TABLE test_t2 FROM test_user; REVOKE pg_example=> REVOKE ALL ON DATABASE pg_example FROM test_user; REVOKE
然後,刪除使用者 test_user︰
pg_example=> DROP ROLE test_user; DROP ROLE
撤銷權限之後,即可成功刪除角色。
相關資訊
關於 DROP ROLE 的 PostgreSQL 文件
相關內容
- 已提問 10 個月前lg...
- 已提問 1 年前lg...
- 已提問 9 個月前lg...
- 已提問 3 個月前lg...
- AWS 官方已更新 2 年前
- AWS 官方已更新 2 年前
- AWS 官方已更新 2 年前
- AWS 官方已更新 1 年前