温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

postgresql删除用户密码的方法

发布时间:2020-07-29 10:39:44 来源:亿速云 阅读:401 作者:清晨 栏目:编程语言

小编给大家分享一下postgresql删除用户密码的方法,相信大部分人都还不怎么了解,因此分享这边文章给大家学习,希望大家阅读完这篇文章后大所收获,下面让我们一起去学习方法吧!

在多租户场景或者其他场景下,很多时候需要主动清理一些用户,本文将介绍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)权限全部回收,才能删除该用户。

对象依赖

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,才能删除该用户。

以上是postgresql删除用户密码的方法的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI