最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PostgreSQL 實現(xiàn)快速刪除一個用戶

 更新時間:2021年01月04日 09:35:47   作者:卓刀  
這篇文章主要介紹了PostgreSQL 實現(xiàn)快速刪除一個用戶,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

背景

在多租戶場景或者其他場景下,很多時候需要主動清理一些用戶,本文將介紹PostgreSQL 下如何快速刪除一個用戶(role)。

具體方法

一般情況下直接執(zhí)行 drop role xxx; 就可以把這個用戶刪除。但是很多時候會因為用戶有依賴而報錯。

權(quán)限依賴

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

可以看出,因為我們把數(shù)據(jù)庫postgres 的權(quán)限賦予了test 用戶,所以直接刪除的時候會報錯。面對這種情況,我們需要先將role 的權(quán)限所有的權(quán)限全部revoke 掉,如下:

postgres=# revoke all on database postgres from test;
REVOKE
postgres=# drop role test;
DROP ROLE

注意:需要把該用戶在所有數(shù)據(jù)庫具有權(quán)限的所有數(shù)據(jù)庫對象的(表,視圖,SEQUENCE)權(quán)限全部回收,才能刪除該用戶。

對象依賴

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

注意:需要把該用戶在所有數(shù)據(jù)庫具有owner 權(quán)限的所有數(shù)據(jù)庫對象(表,視圖,SEQUENCE)刪除或者執(zhí)行alter xx owner to,才能刪除該用戶。

更牛的方法

如果不保留owner 的數(shù)據(jù)庫對象

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ù)庫對象

postgres=# REASSIGN OWNED BY test TO postgres;
REASSIGN OWNED
postgres=# drop role test;
DROP ROLE

注意:REASSIGN OWNED 需要執(zhí)行者所屬的role (或者子集)必須包含test 和postgres 或者是superuser。另外必須所有涉及到的數(shù)據(jù)庫上都執(zhí)行該以上語句才能刪除用戶。

補充:PostgreSQL數(shù)據(jù)庫創(chuàng)建/刪除

方法1 - 系統(tǒng)命令

sudo su - postgres #切換到postgres用戶(系統(tǒng)用戶)
createdb weichen #創(chuàng)建數(shù)據(jù)庫
psql #直接訪問數(shù)據(jù)庫(默認進入本地postgres數(shù)據(jù)庫)
\l --查看數(shù)據(jù)庫列表
:q --退出列表頁面
\q --退出客戶端
dropdb weichen #刪除數(shù)據(jù)庫

方法2 - psql命令行

sudo -u postgres psql #登錄客戶端
create database weichen; --創(chuàng)建數(shù)據(jù)庫
create database sz owner postgres; --創(chuàng)建數(shù)據(jù)庫
select oid,datname from pg_database; --查看數(shù)據(jù)庫列表
drop database weichen; --刪除數(shù)據(jù)庫
drop database sz; --刪除數(shù)據(jù)庫

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關文章

最新評論

铁力市| 洛南县| 珠海市| 云阳县| 铅山县| 龙游县| 萨嘎县| 西安市| 舞钢市| 灌阳县| 枣强县| 乐至县| 淳化县| 宕昌县| 申扎县| 大渡口区| 徐州市| 历史| 宁夏| 光山县| 旌德县| 海南省| 犍为县| 平乡县| 佛坪县| 云龙县| 比如县| 龙山县| 南丰县| 江陵县| 高密市| 阳东县| 东丽区| 永胜县| 会东县| 大埔区| 友谊县| 南涧| 新丰县| 蓬溪县| 兴业县|