开发者社区> 问答> 正文

PostgreSQL 怎样给一个用户授予只读角色?

PostgreSQL 怎样给一个用户授予只读角色?

展开
收起
德哥,digoal 2015-12-29 09:15:10 4579 0
1 条回答
写回答
取消 提交回答
  • 公益是一辈子的事, I am digoal, just do it. 阿里云数据库团队, 擅长PolarDB, PostgreSQL, DuckDB, ADB等, 长期致力于推动开源数据库技术、生态在中国的发展与开源产业人才培养. 曾荣获阿里巴巴麒麟布道师称号、2018届OSCAR开源尖峰人物.
    PostgreSQL没有只读角色这个权限,但是可以通过修改用户的默认参数来达到只读的目的,如下 : 
    digoal=# select usename,useconfig from pg_user where usename='digoal';
     usename | useconfig 
    ---------+-----------
     digoal  | 
    (1 row)
    digoal=# alter role digoal set default_transaction_read_only=true;
    ALTER ROLE
    digoal=# select usename,useconfig from pg_user where usename='digoal';
     usename |              useconfig               
    ---------+--------------------------------------
     digoal  | {default_transaction_read_only=true}
    (1 row)
    连接到digoal用户进行验证,
    digoal=# \c digoal digoal
    You are now connected to database "digoal" as user "digoal".
    digoal=> insert into test values (1);
    ERROR:  cannot execute INSERT in a read-only transaction
    digoal=> delete from test ;
    ERROR:  cannot execute DELETE in a read-only transaction
    digoal=> drop table test ;
    ERROR:  cannot execute DROP TABLE in a read-only transaction
    2019-07-17 18:23:16
    赞同 1 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖大会:开源 PolarDB 架构演进、关键技术与社区建设 立即下载
2023云栖大会:和客户一起玩转PolarDB新特性 立即下载
2023云栖大会:PolarDB for AI 立即下载

相关镜像