我正在尝试从postgres实例高效drop role
,并遇到一些问题。我正在寻找一些SQL drop role
,以便我可以停止阅读错误消息,而不必手动执行此操作。
在我正在使用的设置中,每个角色都有自己的同名数据库:
CREATE ROLE alpha_user;
CREATE DATABASE alpha_user;
ALTER DATABASE alpha_user OWNER TO alpha_user;
用户通常会将对其数据库中的架构的访问权限授予其他用户:
-- alpha_user logs in to the alpha_user database
GRANT USAGE ON SCHEMA myschema TO beta_user;
当我尝试删除beta_user
时,会发生这种情况:
-- log in as superuser
DROP ROLE beta_user;
-- ERROR: role "beta_user" cannot be dropped because some objects depend on it
-- DETAIL: N objects in database alpha_user
我可以连接到alpha_user
数据库,然后删除OWNED BY
,但效率很低:
-- log in as superuser
\c alpha_user;
DROP OWNED BY beta_user CASCADE;
DROP beta_user;
-- success
用户可以授予对任意数量的数据库的访问权限,并且有许多用户。超级用户是否可以在用户被授予访问权限的所有数据库中为用户执行DROP OWNED BY
语句(或一系列语句)?
答案 0 :(得分:1)
也许这会对你有所帮助:
with
user_id as (select oid, rolname as my_user from pg_authid where rolname in('abc', 'xyz'))
select 'REVOKE ' || rolname || ' FROM ' || my_user || ' CASCADE;' as sql
from pg_auth_members
join pg_authid on pg_auth_members.roleid = pg_authid.oid
JOIN user_id ON pg_auth_members.member = user_id.oid
union
SELECT 'REVOKE ALL ON ' || datname || ' FROM ' || my_user || ' CASCADE;'
FROM pg_database
JOIN user_id ON pg_database.datdba = user_id.oid