溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

postgresql數(shù)據(jù)庫刪除用戶的方法

發(fā)布時間:2020-07-18 11:22:43 來源:億速云 閱讀:247 作者:清晨 欄目:編程語言

這篇文章將為大家詳細講解有關(guān)postgresql數(shù)據(jù)庫刪除用戶的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

將介紹PostgreSQL下如何快速刪除一個用戶(role)。

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

推薦:postgresql教程

權(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。如果不需要保留該對象,則需要先把該依賴對象

刪除。如果需要保留該對象,則應(yīng)該在刪除之前先把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,才能

刪除該用戶。

關(guān)于postgresql數(shù)據(jù)庫刪除用戶的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI