1、對(duì)數(shù)據(jù)庫(kù)授權(quán)
postgresql 授權(quán)某個(gè)數(shù)據(jù)庫(kù)的權(quán)限給wang 賬號(hào) 使該賬號(hào) 只能操作指定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; // 起作用的是這句 要進(jìn)入數(shù)據(jù)庫(kù)test 操作,在那個(gè)db環(huán)境執(zhí)行就授哪個(gè)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 ;
對(duì)表授權(quán)
撤銷授權(quán)
撤銷對(duì)數(shù)據(jù)庫(kù)授權(quán)
revoke all on database company from wang; #撤銷用戶wang對(duì)數(shù)據(jù)庫(kù)company 的所有權(quán)限
revoke select on all tables in schema public from wang;
撤銷對(duì)表授權(quán)
對(duì)當(dāng)前庫(kù)中所有表去掉public的所有訪問權(quán)限,為了確保除了所有者之外的洽談?dòng)脩舨荒懿僮鬟@些表。
lyy=# revoke all on test1 from public;
REVOKE
lyy=# revoke all on test2 from public;
REVOKE
去掉對(duì)pg_class的訪問權(quán)限,為了確保yy用戶不能看到所有表名的列表。
lyy=# revoke all on pg_class from public;
REVOKE
lyy=# revoke all on pg_class from yy;
REVOKE
添加yy用戶對(duì)test1表的所屬關(guān)系,確保yy用戶對(duì)test1表有權(quán)限操作
lyy=# ALTER TABLE test1 OWNER TO yy;
lyy=# \q
用戶管理
/* 賦給用戶表的所有權(quán)限 */
GRANT ALL ON tablename TO user;
/* 賦給用戶數(shù)據(jù)庫(kù)的所有權(quán)限 */
GRANT ALL PRIVILEGES ON DATABASE dbname TO dbuser;
/* 撤銷用戶權(quán)限 */
REVOKE privileges ON tablename FROM user;
數(shù)據(jù)庫(kù)操作
/* 創(chuàng)建數(shù)據(jù)庫(kù) */
create database dbname;
/* 刪除數(shù)據(jù)庫(kù) */
drop database dbname;
表操作
/* 增加讓主鍵自增的權(quán)限 */
grant all on sequence tablename_keyname_seq to webuser;
/* 重命名一個(gè)表 */
alter table [表名A] rename to [表名B];
/* 刪除一個(gè)表 */
drop table [表名];
/* 在已有的表里添加字段 */
alter table [表名] add column [字段名] [類型];
/* 刪除表中的字段 */
alter table [表名] drop column [字段名];
/* 重命名一個(gè)字段 */
alter table [表名] rename column [字段名A] to [字段名B];
/* 給一個(gè)字段設(shè)置缺省值 */
alter table [表名] alter column [字段名] set default [新的默認(rèn)值];
/* 去除缺省值 */
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ù)庫(kù)
創(chuàng)建用戶
postgres=# create user username with password '****';
創(chuàng)建數(shù)據(jù)庫(kù)
postgres=# create database dbtest owner username; -- 創(chuàng)建數(shù)據(jù)庫(kù)指定所屬者
將數(shù)據(jù)庫(kù)得權(quán)限,全部賦給某個(gè)用戶
postgres=# grant all on database dbtest to username; -- 將dbtest所有權(quán)限賦值給username
導(dǎo)入整個(gè)數(shù)據(jù)庫(kù)
psql -U username databasename /data/dum.sql -- 用戶名和數(shù)據(jù)庫(kù)名
常見報(bào)錯(cuò) :
1、切換yy用戶失敗
lyy=# \c - yy
FATAL: Peer authentication failed for user "yy"
Previous connection kept
2、用戶yy連接lyyku會(huì)報(bào)錯(cuò)
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對(duì)數(shù)據(jù)庫(kù)lyy的訪問權(quán)限
解決辦法 :
#grant connect on database lyy to yy;
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- 基于PostgreSQL 權(quán)限解讀
- PostgreSQL 默認(rèn)權(quán)限查看方式
- 查看postgresql數(shù)據(jù)庫(kù)用戶系統(tǒng)權(quán)限、對(duì)象權(quán)限的方法
- PostgreSQL教程(十二):角色和權(quán)限管理介紹
- 用一整天的時(shí)間安裝postgreSQL NTFS權(quán)限
- Postgresql 數(shù)據(jù)庫(kù)權(quán)限功能的使用總結(jié)