問題描述
在RDS PostgreSQL控制臺刪除賬號時,出現錯誤提示。其中:
報錯信息:數據庫對象依賴該操作的賬號,請先解除依賴后,再操作。
錯誤碼:
AccountActionForbidden
。
問題原因
在RDS PostgreSQL中,如果存在數據庫對象依賴于即將被刪除的賬號,將導致刪除操作失敗。例如,存在user_to_be_dropped
賬號,并使用該賬號創建了對象。示例SQL如下:
使用高權限賬號,創建測試數據庫
testdb01
。CREATE DATABASE testdb01;
使用高權限賬號,登錄
testdb01
,創建測試賬號user_to_be_dropped
,并賦予測試賬號對應的權限。CREATE USER user_to_be_dropped WITH PASSWORD 'your_password' CREATEROLE CREATEDB; GRANT ALL ON SCHEMA public TO user_to_be_dropped; GRANT ALL ON DATABASE testdb01 TO user_to_be_dropped;
使用賬號
user_to_be_dropped
登錄testdb01
,創建對象和測試數據庫testdb02
。CREATE SCHEMA test_nsp; CREATE TABLE test_nsp.test_tbl(a1 int); CREATE TABLE test_tbl(a2 int); CREATE DATABASE testdb02;
使用賬號
user_to_be_dropped
登錄testdb02
,創建對象和新測試賬號user_to_be_dropped_2
,并給其授予高權限賬號(本文以testdbuser為例)的權限。CREATE SCHEMA testnsp; CREATE TABLE testnsp.tbl(a3 int); CREATE USER user_to_be_dropped_2 WITH PASSWORD 'your_password'; GRANT user_to_be_dropped_2 TO testdbuser;
刪除賬號user_to_be_dropped
時,由于存在依賴對象,所以刪除操作會出現報錯。
RDS PostgreSQL控制臺:
SQL命令:
DROP USER user_to_be_dropped; ERROR: role "user_to_be_dropped" cannot be dropped because some objects depend on it DETAIL: privileges for schema public privileges for database testdb01 owner of schema test_nsp owner of table test_nsp.test_tbl owner of table test_tbl owner of database testdb02 privileges for membership of role testdbuser in role user_to_be_dropped_2 2 objects in database testdb02
解決方案
查找依賴目標賬號的對象,并將其刪除。
查找依賴對象
系統表pg_shdepend
中記錄了單個數據庫內部的對象對全局對象的依賴,可以用于查詢哪些對象依賴了目標賬號。例如,查詢哪些對象依賴賬號 user_to_be_dropped
。
使用高權限賬號登錄數據庫實例,查詢與賬號
user_to_be_dropped
相關聯的對象。WITH role as (SELECT oid FROM pg_roles WHERE rolname = 'user_to_be_dropped') SELECT db.datname AS database, pg_class.relname AS classname, shp.objid AS oid, CASE WHEN shp.deptype = 'o' THEN 'Object Owner' WHEN shp.deptype = 'a' THEN 'In Access Control List' WHEN shp.deptype = 'r' THEN 'Policy Object' ELSE 'CANNOT HAPPEN' END FROM pg_shdepend shp LEFT JOIN pg_database db ON shp.dbid = db.oid JOIN pg_class ON shp.classid = pg_class.oid WHERE shp.refclassid = 1260 AND shp.refobjid IN (SELECT oid FROM role);
查詢結果為:
database | classname | oid | case ----------+-----------------+-------+------------------------ testdb01 | pg_namespace | 2200 | In Access Control List | pg_database | 16399 | In Access Control List testdb01 | pg_namespace | 16402 | Object Owner testdb01 | pg_class | 16403 | Object Owner testdb01 | pg_class | 16406 | Object Owner | pg_database | 16409 | Object Owner testdb02 | pg_namespace | 16410 | Object Owner testdb02 | pg_class | 16411 | Object Owner | pg_auth_members | 16416 | In Access Control List
其中各個字段的含義如下。
字段
含義
database
指明依賴對象所在的庫,為空時表示全局對象的依賴。
classname
指明系統表名稱。
oid
指明依賴對象的oid。
case
指明依賴類型,常見的類型包括:
Owner:被刪除賬號是該對象的所有者。
ACL(Access Control List):被刪除賬號在Access Control List中。
使用高權限賬號登錄到對應數據庫中,在對應的系統表中查詢上一步獲得的
oid
所對應的對象名稱,例如。全局對象的依賴
database
字段為空,表示全局對象的依賴。oid=16399
,testdb01
的ACL中有user_to_be_dropped
的相關條目。SELECT datname, datdba::regrole, datacl FROM pg_database WHERE oid = 16399; datname | datdba | datacl ----------+------------+------------------------------------------------------------------------------ testdb01 | testdbuser | {=Tc/testdbuser,testdbuser=CTc/testdbuser,user_to_be_dropped=CTc/testdbuser} (1 row)
oid=16409
,testdb02
的所有者為user_to_be_dropped
。SELECT datname, datdba::regrole, datacl FROM pg_database WHERE oid = 16409; datname | datdba | datacl ----------+--------------------+-------- testdb02 | user_to_be_dropped | (1 row)
oid=16416
,pg_auth_members
中的角色從屬關系,記錄了授予user_to_be_dropped
賬號的相應權限。SELECT oid, roleid::regrole, member::regrole, grantor::regrole FROM pg_auth_members WHERE oid = 16416; oid | roleid | member | grantor -------+----------------------+------------+-------------------- 16416 | user_to_be_dropped_2 | testdbuser | user_to_be_dropped (1 row)
單庫內對象的依賴
database
字段不為空,表示該庫下存在依賴對象。oid=2200
,testdb01
下public schema
的ACL中有user_to_be_dropped
的相關條目。SELECT nspname, nspowner::regrole, nspacl FROM pg_namespace WHERE oid = 2200; nspname | nspowner | nspacl ---------+------------+--------------------------------------------------------------------------- public | testdbuser | {testdbuser=UC/testdbuser,=U/testdbuser,user_to_be_dropped=UC/testdbuser} (1 row)
oid=16403
,testdb01
下的表test_nsp.test_tbl
的所有者為user_to_be_dropped
。SELECT relname, relnamespace::regnamespace, relowner::regrole, relacl FROM pg_class WHERE oid = 16403; relname | relnamespace | relowner | relacl ----------+--------------+--------------------+-------- test_tbl | test_nsp | user_to_be_dropped | (1 row)
處理依賴對象
您可以根據實際需求:
如果是ACL類型的依賴,可以收回對應的權限。
如果是Owner類型的依賴,可以將其所有權轉移給其他賬號,也可以刪除對應的依賴。
例如:
ACL類型的依賴
oid=2200
,收回testdb01
下public schema
的權限。SELECT nspname, nspowner::regrole, nspacl FROM pg_namespace WHERE oid = 2200; nspname | nspowner | nspacl ---------+------------+--------------------------------------------------------------------------- public | testdbuser | {testdbuser=UC/testdbuser,=U/testdbuser,user_to_be_dropped=UC/testdbuser} (1 row) REVOKE ALL ON SCHEMA public FROM user_to_be_dropped;
oid=16399
,收回testdb01
的權限。SELECT datname, datdba::regrole, datacl FROM pg_database WHERE oid = 16399; datname | datdba | datacl ----------+------------+------------------------------------------------------------------------------ testdb01 | testdbuser | {=Tc/testdbuser,testdbuser=CTc/testdbuser,user_to_be_dropped=CTc/testdbuser} (1 row) REVOKE ALL ON DATABASE testdb01 from user_to_be_dropped;
oid=16416
,對于包含角色從屬關系的情況,RDS PostgreSQL 16 及以上版本中,需要切換到user_to_be_dropped
賬號,刪除user_to_be_dropped_2
的高權限賬號(本文以testdbuser為例)的權限。\c testdb01 user_to_be_dropped; You are now connected to database "testdb01" as user "user_to_be_dropped". REVOKE user_to_be_dropped_2 FROM testdbuser cascade;
Owner類型依賴所有權轉移給其他賬號。
使用高權限賬單登錄到對應數據庫。
將對象的所有權轉移給其他賬號。
oid=16403
,將testdb01
下的表test_nsp.test_tbl
的所有者權限轉移給其他賬號(本文以testdbuser為例)。SELECT relname, relnamespace::regnamespace, relowner::regrole, relacl FROM pg_class WHERE oid = 16403; relname | relnamespace | relowner | relacl ----------+--------------+--------------------+-------- test_tbl | test_nsp | user_to_be_dropped | (1 row) ALTER TABLE test_nsp.test_tbl OWNER TO testdbuser;
刪除依賴對象。
刪除測試數據庫testdb02。
DROP DATABASE testdb02;
刪除
oid=16402
的對象。SELECT nspname, nspowner::regrole, nspacl FROM pg_namespace WHERE oid = 16402; nspname | nspowner | nspacl ----------+--------------------+-------- test_nsp | user_to_be_dropped | (1 row) DROP SCHEMA test_nsp cascade;
刪除
oid=16406
的對象。SELECT relname, relnamespace::regnamespace, relowner::regrole, relacl FROM pg_class WHERE oid = 16406; relname | relnamespace | relowner | relacl ----------+--------------+--------------------+-------- test_tbl | public | user_to_be_dropped | (1 row) DROP TABLE public.test_tbl;
使用高權限賬號登錄數據庫實例,查詢與賬號 user_to_be_dropped相關聯的對象。
返回結果:
database | classname | oid | case
----------+-----------+-----+------
(0 rows)
刪除目標賬號
在RDS PostgreSQL控制臺或通過SQL命令刪除目標賬號。SQL示例如下。
DROP USER user_to_be_dropped;