postgresql数据库基础
admin
2023-05-28 20:41:24
0

创建只读账号

1.1以初始化账号登入

[root@localhost ~]# psql -U postgres

1.2创建用户

postgres=# create role develop with login password '123456';  

CREATE ROLE

postgres=# select usename from pg_user;

 usename  

----------

 postgres

 test

 develop

(3 rows)


1.3切换数据库

\c current_product

1.4赋予只读权限

current_product=# grant select on all tables in schema public to develop;

GRANT

1.5切换到develop用户

current_product=# \c - develop

You are now connected to database "current_product" as user "develop".

1.6检测是否拥有只读权限

current_product=> select * from test;    

 id 

----

(0 rows)


2创建读写账号

2.1初始账号登录

psql -U postgres   

2.2查看用户

postgres=# select usename from pg_user;

 usename  

----------

 postgres

 test

 test1

 u2

(4 rows)


2.3创建读写用户

postgres=# create role test2 with login password '123456';

CREATE ROLE

postgres=# grant ALL  on all tables in schema public to test2;  #这种授权方式是不对的,test2用户对current_product数据库没有权限

GRANT


2.4检测用户是否有读写权限

postgres=# \c - test2

You are now connected to database "postgres" as user "test2".

切换数据库

postgres=> \c current_product  

You are now connected to database "current_product" as user "test2".

current_product=> \dt

        List of relations

 Schema | Name | Type  |  Owner   

--------+------+-------+----------

 public | aaa  | table | postgres

 public | test | table | postgres

(2 rows)


current_product=> select * from aaa;         #显示没有权限

ERROR:  permission denied for relation aaa


2.5 正确的授权方式是 :切换到目标数据库,执行授权语句

postgres=# \c current_product     #切换到目标数据库

You are now connected to database "current_product" as user "postgres".

current_product=# grant ALL  on all tables in schema public to test2;   #执行授权语句

GRANT


2.6 切换到读写用户,检测是否有权限

current_product=# \c - test2       ###切换至读写用户

You are now connected to database "current_product" as user "test2".

current_product=> \dt     ###查看几个表

        List of relations

 Schema | Name | Type  |  Owner   

--------+------+-------+----------

 public | aaa  | table | postgres

 public | test | table | postgres

(2 rows)


current_product=> select * from aaa;    #查权限正常

 id 

----

(0 rows)


current_product=> insert into aaa values(1);  #增权限正常

INSERT 0 1

current_product=> select * from aaa;        

 id 

----

  1

(1 row)


current_product=> delete from aaa;   #删除权限正常

DELETE 1


2.7 切换至超级用户

current_product=> \c - postgres

You are now connected to database "current_product" as user "postgres".

current_product=# create table bbb(id int);     ###新增一张表

CREATE TABLE


2.8 切换至读写用户

current_product=# \c - test2

You are now connected to database "current_product" as user "test2".

current_product=> \dt

        List of relations

 Schema | Name | Type  |  Owner   

--------+------+-------+----------

 public | aaa  | table | postgres

 public | bbb  | table | postgres

 public | test | table | postgres

(3 rows)


current_product=> select * from bbb;     #显示无权限

ERROR:  permission denied for relation bbb


2.9 解决办法:

每次新增表都执行一次授权语句,否则无权限(其它方法正在探索中……)

current_product=> \c - postgres

You are now connected to database "current_product" as user "postgres".

current_product=# grant ALL  on all tables in schema public to test2;

GRANT

切换至读写用户 , 检测权限

current_product=# \c - test2

You are now connected to database "current_product" as user "test2".

current_product=> select * from bbb;

 id 

----

(0 rows) 

current_product=> insert into bbb  values(2222);

INSERT 0 1

current_product=> select * from bbb;

  id  

------

 2222

(1 row)


current_product=> delete from bbb;

DELETE 1

current_product=> select * from bbb;

 id 

----

(0 rows)


相关内容

热门资讯

重新定义信号与频谱分析仪:罗德... 全球测试测量专家罗德与施瓦茨(以下简称“R&S”)宣布,将于6月23日举办 “新一代多通道信号与频谱...
美媒:伊朗军方重建速度远超预期 据美国有线电视新闻网(CNN)报道,两名熟悉美国情报评估的消息人士透露,伊朗已在今年4月初开始的六周...
泰国取消60天免签,是想防着谁... 【文/观察者网专栏作者 岳汉】最近,泰国缩短免签这个事,在国内还引起了挺大关注。很多中国朋友都不解:...
170次要变1万次,马斯克“太... 【文/观察者网 王一】美国企业家马斯克为其旗下太空探索技术公司(SpaceX)制定了一个宏大的目标—...
现在是中国军工进入海湾的新契机... 【文/观察者网专栏作者 晨枫】美以伊战事的走向和终局仍扑朔迷离。长期依赖美国保护伞的海湾国家,其“石...
商务部:中美双方应为双向农产品... 新华社北京5月21日电(记者谢希瑶、何晓)商务部新闻发言人何亚东21日在例行新闻发布会上,回答有关中...
红墙藏旧韵,老厂焕新潮|郑州合... 当城市更新的浪潮邂逅珍贵工业遗存,斑驳红砖墙不再只剩岁月沉淀,更悄然生长出潮流时尚与市井烟火。坐落于...
河南继续雨雨雨模式,警惕持续降... 天气回顾昨天(20日)白天到夜里,河南大部雨水暂歇,仅北部和南部地区出现了分散性阵雨、雷阵雨;不过由...
韩国警方:金秀贤“与未成年人交... 据报道,韩国警方一份记录披露,此前韩国著名演员金秀贤“与未成年交往”系不实消息。韩国首尔江南警察厅已...
海关总署发布支持大湾区建设若干... 今天(21日),海关总署举行新闻发布会,就近日出台的《海关支持粤港澳大湾区建设若干措施》进行解读,从...