一、具体方法

一般情况下直接执行 drop role xxx; 就可以把这个用户删除。但是很多时候会因为用户有依赖而报错。

二、权限依赖

postgres=# create role test with login;

CREATE ROLE
postgres=# grant all on database postgres to test;
GRANT
postgres=# drop role test;
ERROR: role "test" cannot be dropped because some objects depend on it
DETAIL: privileges for database postgres
可以看出,因为我们把数据库postgres 的权限赋予了test 用户,所以直接删除的时候会报错。面对这种情况,我们需要先将role 的权限所有的权限全部revoke 掉,如下:
postgres=# revoke all on database postgres from test;

REVOKE
postgres=# drop role test;
DROP ROLE
注意:需要把该用户在所有数据库具有权限的所有数据库对象的(表,视图,SEQUENCE)权限全部回收,才能删除该用户。

三、对象依赖

postgres=# create role test with login;

CREATE ROLE
postgres=# \c - test
You are now connected to database "postgres" as user "test".
postgres=> create table test (id int);
CREATE TABLE
postgres=# \c - postgres
You are now connected to database "postgres" as user "postgres".
postgres=# drop role test;
ERROR: role "test" cannot be dropped because some objects depend on it
DETAIL: owner of table test
可以看出,因为test 用户是test 表的owner,所以删除的时候报错owner of table test。如果不需要保留该对象,则需要先把该依赖对象删除。如果需要保留该对象,则应该在删除之前先把owner 赋予别人,如下:
postgres=# alter table test OWNER TO postgres;

ALTER TABLE
postgres=# drop role test;
DROP ROLE
注意:需要把该用户在所有数据库具有owner 权限的所有数据库对象(表,视图,SEQUENCE)删除或者执行alter xx owner to,才能删除该用户。

四、更改数据库角色拥有的数据库对象的所有权

1、如果不保留owner 的数据库对象

postgres=# REASSIGN OWNED BY test TO postgres;

REASSIGN OWNED
postgres=# DROP OWNED BY test;
DROP OWNED
postgres=# drop role test;
DROP ROLE
2、如果保留owner 的数据库对象
postgres=# REASSIGN OWNED BY test TO postgres;

REASSIGN OWNED
postgres=# drop role test;
DROP ROLE
注意:REASSIGN OWNED 需要执行者所属的role (或者子集)必须包含test 和postgres 或者是superuser。另外必须所有涉及到的数据库上都执行该以上语句才能删除用户。
 

最新文章

  1. 请写一个php函数,可以接受任意数量的参数
  2. Linux学习心得之 Linux下ant安装与使用
  3. Jetty官方文档翻译
  4. ASCII
  5. “wsimport -keep ”生成客户端报错“Use of SOAP Encoding is not supported.”
  6. 改变bootstarp图标水平方向
  7. JavaWeb项目开发案例精粹-第6章报价管理系统-04Service层
  8. Java从入门到精通——数据库篇之OJDBC版本区别
  9. PDB符号文件信息
  10. openstack nova修改实例路径,虚拟磁盘路径
  11. Node.js学习 - Function
  12. 初识在Spring Boot中使用JPA
  13. AndroidVerifyBoot
  14. Java Spring MVC框架搭建(一)
  15. react-native 报错 RawText "" must be wrapped in an explicit <Text> component
  16. 2018-01-19 Xtext试用: 5步实现一个(中文)JVM语言
  17. RF-For循环使用
  18. ajax 实现跨域
  19. task:scheduled cron 合法
  20. 一键安装lnmp-nginx(3)

热门文章

  1. 记一次InputStream流读取不完整留下的惨痛教训
  2. Android-helloword
  3. IDEA引入本地jar包的几种方法
  4. 软件工程大作业——“你帮我助”软件开发v2.0
  5. Ubuntu:Docker启动与停止
  6. JavaScript:七大基础数据类型:布尔值boolean、空null、未定义undefined
  7. C++11(列表初始化+变量类型推导+类型转换+左右值概念、引用+完美转发和万能应用+定位new+可变参数模板+emplace接口)
  8. java后端整合极光消息推送
  9. [OpenCV实战]1 基于深度学习识别人脸性别和年龄
  10. 使用C语言编程的7个步骤