postgresql 授權(quán)某個數(shù)據(jù)庫的權(quán)限給wang 賬號 使該賬號 只能操作指定DB 不能操作其他DB
alter user wang set default_transaction_read_only=on; grant all on database test to wang; grant select on all database test to wang; grant select on all tables in schema public to wang; // 起作用的是這句 要進入數(shù)據(jù)庫test 操作,在那個db環(huán)境執(zhí)行就授哪個db的權(quán)
配置權(quán)限
ve=# grant all on schema public to foo ; ve=# grant select,insert,update,delete on test to foo ; ve=# grant select,insert,update,delete on public.test to foo ;
對表授權(quán)
撤銷授權(quán)
撤銷對數(shù)據(jù)庫授權(quán)
revoke all on database company from wang; #撤銷用戶wang對數(shù)據(jù)庫company 的所有權(quán)限 revoke select on all tables in schema public from wang;
撤銷對表授權(quán)
對當(dāng)前庫中所有表去掉public的所有訪問權(quán)限,為了確保除了所有者之外的洽談用戶不能操作這些表。
lyy=# revoke all on test1 from public; REVOKE lyy=# revoke all on test2 from public; REVOKE
去掉對pg_class的訪問權(quán)限,為了確保yy用戶不能看到所有表名的列表。
lyy=# revoke all on pg_class from public; REVOKE lyy=# revoke all on pg_class from yy; REVOKE
添加yy用戶對test1表的所屬關(guān)系,確保yy用戶對test1表有權(quán)限操作
lyy=# ALTER TABLE test1 OWNER TO yy; lyy=# \q
用戶管理
/* 賦給用戶表的所有權(quán)限 */ GRANT ALL ON tablename TO user; /* 賦給用戶數(shù)據(jù)庫的所有權(quán)限 */ GRANT ALL PRIVILEGES ON DATABASE dbname TO dbuser; /* 撤銷用戶權(quán)限 */ REVOKE privileges ON tablename FROM user;
數(shù)據(jù)庫操作
/* 創(chuàng)建數(shù)據(jù)庫 */ create database dbname; /* 刪除數(shù)據(jù)庫 */ drop database dbname;
表操作
/* 增加讓主鍵自增的權(quán)限 */ grant all on sequence tablename_keyname_seq to webuser; /* 重命名一個表 */ alter table [表名A] rename to [表名B]; /* 刪除一個表 */ drop table [表名]; /* 在已有的表里添加字段 */ alter table [表名] add column [字段名] [類型]; /* 刪除表中的字段 */ alter table [表名] drop column [字段名]; /* 重命名一個字段 */ alter table [表名] rename column [字段名A] to [字段名B]; /* 給一個字段設(shè)置缺省值 */ alter table [表名] alter column [字段名] set default [新的默認值]; /* 去除缺省值 */ alter table [表名] alter column [字段名] drop default; /* 插入數(shù)據(jù) */ insert into 表名 ([字段名m],[字段名n],......) values ([列m的值],[列n的值],......); /* 修改數(shù)據(jù) */ update [表名] set [目標(biāo)字段名]=[目標(biāo)值] where ...; /* 刪除數(shù)據(jù) */ delete from [表名] where ...; /* 刪除表 */ delete from [表名]; /* 查詢 */ SELECT * FROM dbname WHERE ...; /* 創(chuàng)建表 */ create table ( [字段名1] [類型1] primary key,
參考
創(chuàng)建用戶和數(shù)據(jù)庫
創(chuàng)建用戶
postgres=# create user username with password '****';
創(chuàng)建數(shù)據(jù)庫
postgres=# create database dbtest owner username; -- 創(chuàng)建數(shù)據(jù)庫指定所屬者
將數(shù)據(jù)庫得權(quán)限,全部賦給某個用戶
postgres=# grant all on database dbtest to username; -- 將dbtest所有權(quán)限賦值給username
導(dǎo)入整個數(shù)據(jù)庫
psql -U username databasename /data/dum.sql -- 用戶名和數(shù)據(jù)庫名
1、切換yy用戶失敗
lyy=# \c - yy FATAL: Peer authentication failed for user "yy" Previous connection kept
2、用戶yy連接lyyku會報錯
psql -E -U yy -d lyy Password for user yy: psql: FATAL: permission denied for database "lyy" DETAIL: User does not have CONNECT privilege.
原因 :沒有connect權(quán)限,那么就授予用戶yy對數(shù)據(jù)庫lyy的訪問權(quán)限
解決辦法 :
#grant connect on database lyy to yy;
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
標(biāo)簽:株洲 錦州 來賓 晉城 珠海 衡陽 蚌埠 烏海
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Postgresql 賦予用戶權(quán)限和撤銷權(quán)限的實例》,本文關(guān)鍵詞 Postgresql,賦予,用戶,權(quán)限,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。