postgresql怎么删除用户密码
2023-04-18 14:24:16 时间
在多租户场景或者其他场景下,很多时候需要主动清理一些用户,本文将介绍PostgreSQL 下如何快速删除一个用户(role)。
具体方法
一般情况下直接执行 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)权限全部回收,才能删除该用户。
推荐学习《python教程》。
对象依赖
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,才能
删除该用户。
相关文章
- FreeRTOS-05-队列
- 热门数据集提供【MNIST、鸢尾花、猫狗、CIFAR10、vegetables、Ox-Flowers17、pascalvoc】
- SHA256/SHA512码计算方式
- 【四】AI Studio 项目详解【VisualDL工具、环境使用说明、脚本任务、(四)图形化任务、在线部署及预测】PARL
- 大小端判断
- 编码测试文件【1】
- 《Qt学习系列笔记》--章节索引
- Qt-数据库操作SQLite
- Qt-udp通信
- [转发]MySQL安装配置教程(超级详细、保姆级)
- 计算机中数的表示
- MySQL常用操作指令大全
- numpy基础--通用函数:快速的元素级数组函数
- 网络协议栈基本知识
- FastAPI 学习之路(五十五)操作Redis
- FastAPI 学习之路(五十三)根据环境不同连接不同数据库
- FastAPI 学习之路(三十四)数据库多表操作
- FastAPI 学习之路(三十三)操作数据库
- FastAPI 学习之路(三十二)创建数据库
- spring boot 集成Druid