濮阳杆衣贸易有限公司

主頁 > 知識庫 > PostgreSQL 實現(xiàn)快速刪除一個用戶

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

熱門標簽:漯河外呼電話系統(tǒng) 海豐有多少商家沒有地圖標注 地圖標注和圖片名稱的區(qū)別 重慶自動外呼系統(tǒng)定制 辦公外呼電話系統(tǒng) 外呼調(diào)研系統(tǒng) 合肥公司外呼系統(tǒng)運營商 打電話智能電銷機器人授權 美容工作室地圖標注

背景

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

具體方法

一般情況下直接執(zhí)行 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

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

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

注意:需要把該用戶在所有數(shù)據(jù)庫具有權限的所有數(shù)據(jù)庫對象的(表,視圖,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

注意:需要把該用戶在所有數(shù)據(jù)庫具有owner 權限的所有數(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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • postgresql 刪除重復數(shù)據(jù)的幾種方法小結(jié)
  • PostgreSQL 刪除check約束的實現(xiàn)
  • postgresql刪除主鍵的操作
  • postgresql 實現(xiàn)多表關聯(lián)刪除
  • Postgresql創(chuàng)建新增、刪除與修改觸發(fā)器的方法
  • PostgreSQL刪除更新優(yōu)化操作
  • mybatis postgresql 批量刪除操作方法
  • postgresql 刪除重復數(shù)據(jù)案例詳解

標簽:來賓 錦州 蚌埠 衡陽 烏海 晉城 珠海 株洲

巨人網(wǎng)絡通訊聲明:本文標題《PostgreSQL 實現(xiàn)快速刪除一個用戶》,本文關鍵詞  PostgreSQL,實現(xiàn),快速,刪除,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PostgreSQL 實現(xiàn)快速刪除一個用戶》相關的同類信息!
  • 本頁收集關于PostgreSQL 實現(xiàn)快速刪除一個用戶的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    武功县| 平阴县| 红桥区| 开鲁县| 屏东县| 长岛县| 云安县| 夹江县| 宜章县| 兴和县| 嘉定区| 布尔津县| 金昌市| 西林县| 乌拉特后旗| 东海县| 维西| 开封市| 师宗县| 皮山县| 尉犁县| 七台河市| 桂阳县| 旬邑县| 石嘴山市| 杭锦后旗| 赣榆县| 吉水县| 宁波市| 宜丰县| 荥阳市| 和林格尔县| 嘉义县| 博乐市| 津南区| 定安县| 渝中区| 十堰市| 滦平县| 民权县| 易门县|