如果用户具有依赖对象,如何删除用户的postgres

时间:2015-10-07 09:30:50

标签: sql postgresql plpgsql

数据库IDd所有者是角色idd_owner

数据库有2个数据模式:publicfirma1。 用户可能直接或间接地在此数据库和对象中分配了权限。 用户不是任何对象的所有者。它只授予了权利。

如何删除此类用户?

我试过

revoke all on all tables in schema public,firma1 from "vantaa" cascade;
revoke all on all sequences in schema public,firma1 from "vantaa" cascade;
revoke all on database idd from "vantaa" cascade;
revoke all on all functions in schema public,firma1 from "vantaa" cascade;
revoke all on schema public,firma1 from "vantaa" cascade;
revoke idd_owner from "vantaa" cascade;
ALTER DEFAULT PRIVILEGES IN SCHEMA public,firma1 revoke all ON TABLES from "vantaa";
DROP ROLE if exists "vantaa"

但收到错误

role "vantaa" cannot be dropped because some objects depend on it
DETAIL:  privileges for schema public
DROP ROLE if exists "vantaa"

如何修复此问题以便用户可以放弃?

如何创建sql或plpgsql方法,该方法将用户名作为参数并在所有情况下删除此用户而不丢弃数据?

使用Postgres 9.1 +

2 个答案:

答案 0 :(得分:6)

在删除用户之前,您可以运行:

REASSIGN OWNED BY vantaa TO <newuser>

如果您不知道将该人重新分配给...,您可以重新分配给postgres ...

REASSIGN OWNED BY vantaa TO postgres;

答案 1 :(得分:4)

您的重新分配可能会失败。 当您重新分配给新用户/角色时   - 您在当前选定的数据库上执行此操作   - 当前用户必须属于&#39;来自&#39;角色和&#39;到&#39; (授予他们并在之后撤销)

我的shell脚本的摘录(省略了一些事情)

# The DROP USER operation will fail if this user is the owner of an existing schema, table, index...
# The user creating a resource owns it. It should transfer the ownership to the role
# When reassigning, the current user needs to have the <<roles>> associated to BY and TO to execute the command.
GRANT $_roleservice TO $_superuser;                       
GRANT $_account TO $_superuser;                                                                
REASSIGN OWNED BY $_account TO $_roleservice;            
# dropping the user removes it from the current user list of roles/users
DROP USER $_account;  
REVOKE $_roleservice FROM $_superuser;