PostgreSQL 實(shí)現(xiàn)快速刪除一個(gè)用戶(hù)
背景
在多租戶(hù)場(chǎng)景或者其他場(chǎng)景下,很多時(shí)候需要主動(dòng)清理一些用戶(hù),本文將介紹PostgreSQL 下如何快速刪除一個(gè)用戶(hù)(role)。
具體方法
一般情況下直接執(zhí)行 drop role xxx; 就可以把這個(gè)用戶(hù)刪除。但是很多時(shí)候會(huì)因?yàn)橛脩?hù)有依賴(lài)而報(bào)錯(cuò)。
權(quán)限依賴(lài)
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
可以看出,因?yàn)槲覀儼褦?shù)據(jù)庫(kù)postgres 的權(quán)限賦予了test 用戶(hù),所以直接刪除的時(shí)候會(huì)報(bào)錯(cuò)。面對(duì)這種情況,我們需要先將role 的權(quán)限所有的權(quán)限全部revoke 掉,如下:
postgres=# revoke all on database postgres from test; REVOKE postgres=# drop role test; DROP ROLE
注意:需要把該用戶(hù)在所有數(shù)據(jù)庫(kù)具有權(quán)限的所有數(shù)據(jù)庫(kù)對(duì)象的(表,視圖,SEQUENCE)權(quán)限全部回收,才能刪除該用戶(hù)。
對(duì)象依賴(lài)
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
可以看出,因?yàn)閠est 用戶(hù)是test 表的owner,所以刪除的時(shí)候報(bào)錯(cuò)owner of table test。如果不需要保留該對(duì)象,則需要先把該依賴(lài)對(duì)象刪除。如果需要保留該對(duì)象,則應(yīng)該在刪除之前先把owner 賦予別人,如下:
postgres=# alter table test OWNER TO postgres; ALTER TABLE postgres=# drop role test; DROP ROLE
注意:需要把該用戶(hù)在所有數(shù)據(jù)庫(kù)具有owner 權(quán)限的所有數(shù)據(jù)庫(kù)對(duì)象(表,視圖,SEQUENCE)刪除或者執(zhí)行alter xx owner to,才能刪除該用戶(hù)。
更牛的方法
如果不保留owner 的數(shù)據(jù)庫(kù)對(duì)象
postgres=# REASSIGN OWNED BY test TO postgres; REASSIGN OWNED postgres=# DROP OWNED BY test; DROP OWNED postgres=# drop role test; DROP ROLE
如果保留owner 的數(shù)據(jù)庫(kù)對(duì)象
postgres=# REASSIGN OWNED BY test TO postgres; REASSIGN OWNED postgres=# drop role test; DROP ROLE
注意:REASSIGN OWNED 需要執(zhí)行者所屬的role (或者子集)必須包含test 和postgres 或者是superuser。另外必須所有涉及到的數(shù)據(jù)庫(kù)上都執(zhí)行該以上語(yǔ)句才能刪除用戶(hù)。
補(bǔ)充:PostgreSQL數(shù)據(jù)庫(kù)創(chuàng)建/刪除
方法1 - 系統(tǒng)命令
sudo su - postgres #切換到postgres用戶(hù)(系統(tǒng)用戶(hù)) createdb weichen #創(chuàng)建數(shù)據(jù)庫(kù) psql #直接訪(fǎng)問(wèn)數(shù)據(jù)庫(kù)(默認(rèn)進(jìn)入本地postgres數(shù)據(jù)庫(kù)) \l --查看數(shù)據(jù)庫(kù)列表 :q --退出列表頁(yè)面 \q --退出客戶(hù)端 dropdb weichen #刪除數(shù)據(jù)庫(kù)
方法2 - psql命令行
sudo -u postgres psql #登錄客戶(hù)端 create database weichen; --創(chuàng)建數(shù)據(jù)庫(kù) create database sz owner postgres; --創(chuàng)建數(shù)據(jù)庫(kù) select oid,datname from pg_database; --查看數(shù)據(jù)庫(kù)列表 drop database weichen; --刪除數(shù)據(jù)庫(kù) drop database sz; --刪除數(shù)據(jù)庫(kù)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Windows?環(huán)境搭建?PostgreSQL?邏輯復(fù)制高可用架構(gòu)數(shù)據(jù)庫(kù)服務(wù)
本文主要介紹Windows下搭建PostgreSQL的主從邏輯復(fù)制,關(guān)于PostgreSQl的相關(guān)運(yùn)維文章,網(wǎng)絡(luò)上大多都是?Linux?環(huán)境下的操作,鮮有在?Windows?環(huán)境下配置的教程,所以本文采用?Windows?環(huán)境作為演示系統(tǒng)來(lái)進(jìn)行?PostgreSQL?高可用數(shù)據(jù)庫(kù)服務(wù)的搭建,感興趣的朋友一起看看吧2023-05-05PostgreSQL pg_archivecleanup與清理archivelog的操作
這篇文章主要介紹了PostgreSQL pg_archivecleanup與清理archivelog的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01在 PostgreSQL中解決圖片二進(jìn)制數(shù)據(jù)由于bytea_output參數(shù)問(wèn)題導(dǎo)致顯示不正常的問(wèn)題
無(wú)論 bytea_output 參數(shù)設(shè)置為 hex 還是 escape,你都可以通過(guò) C# 訪(fǎng)問(wèn) PostgreSQL 數(shù)據(jù)庫(kù),并且正常獲取并顯示圖片,本篇隨筆介紹這個(gè)問(wèn)題的處理過(guò)程,感興趣的朋友跟隨小編一起看看吧2024-03-03PostgreSQL查找并刪除重復(fù)數(shù)據(jù)的方法總結(jié)
這篇文章主要給大家介紹了PostgreSQL查找并刪除重復(fù)數(shù)據(jù)的方法,文章通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一點(diǎn)的幫助,需要的朋友可以參考下2023-10-1015個(gè)postgresql數(shù)據(jù)庫(kù)實(shí)用命令分享
這篇文章主要介紹了15個(gè)實(shí)用的postgresql數(shù)據(jù)庫(kù)命令分享,都是一些技巧性的postgresql命令,需要的朋友可以參考下2014-07-07使用PostgreSQL創(chuàng)建高級(jí)搜索引擎的代碼示例
本文我們將探索PostgreSQL中的全文搜索功能,并研究我們能夠復(fù)制多少典型搜索引擎功能,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2023-07-07postgresql如何找到表中重復(fù)數(shù)據(jù)的行并刪除
這篇文章主要介紹了postgresql如何找到表中重復(fù)數(shù)據(jù)的行并刪除問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05