PostgreSQL创建只读账户
目前PostgreSQL并不能像MySQL一样直接对某个数据库赋予只读权限,现实中有研发需要新建一个用户然后赋予对某个数据库只读权限。
举例说明如何创建
用edbstore用户连接edbstore数据库,并创建一个测试schema
$ psql -U edbstore edbstore
psql (9.6.4)
Type "help" for help. edbstore=> create schema ota_data;
CREATE SCHEMA
edbstore=> \dn ota_data
List of schemas
Name | Owner
----------+----------
ota_data | edbstore
(1 row)
在测试schema下创建一张测试表并插入部分数据
edbstore=> set search_path to ota_data ;
SET
edbstore=> show search_path ;
search_path
-------------
ota_data
(1 row) edbstore=> create table tb1(name varchar,age int);
CREATE TABLE
edbstore=> insert into tb1 values('chris',23);
INSERT 0 1
edbstore=> insert into tb1 values('tonny',25);
INSERT 0 1
edbstore=> select * from tb1;
name | age
-------+-----
chris | 23
tonny | 25
(2 rows)
现在新建一个readonly用户并尝试访问edbstore用户下新建的测试数据
postgres=# create role readonly password 'readonly' login;
CREATE ROLE
$ export PGPASSWORD=readonly
$ psql -h 172.16.101.66 -U readonly edbstore
psql (9.6.)
Type "help" for help. edbstore=> set search_path to ota_data ;
SET
edbstore=> \d
No relations found.
可以看到默认情况下新用户readonly是无法看到edbstore用户下的数据的,现在赋予用户readonly查看schema对象的权限
edbstore=> GRANT USAGE ON SCHEMA ota_data TO readonly;
GRANT
readonly用户再次查看
edbstore=> \d
List of relations
Schema | Name | Type | Owner
----------+------+-------+----------
ota_data | tb1 | table | edbstore
(1 row) edbstore=> select * from tb1 ;
ERROR: permission denied for relation tb1
可以看到虽然用户可以查看到该对象,但是没用select权限,现在进行赋权
edbstore=> \dp
Access privileges
Schema | Name | Type | Access privileges | Column privileges | Policies
----------+------+-------+-------------------+-------------------+----------
ota_data | tb1 | table | | |
(1 row) edbstore=> GRANT SELECT ON ALL TABLES IN SCHEMA ota_data TO readonly;
GRANT
edbstore=> \dp
Access privileges
Schema | Name | Type | Access privileges | Column privileges | Policies
----------+------+-------+---------------------------+-------------------+----------
ota_data | tb1 | table | edbstore=arwdDxt/edbstore+| |
| | | readonly=r/edbstore | |
(1 row)
readonly用户再次查看
edbstore=> select * from tb1 ;
name | age
-------+-----
chris | 23
tonny | 25
(2 rows) edbstore=> insert into tb1 values('tina',19);
ERROR: permission denied for relation tb1
该命令只能对已经存在的对象(表,视图等)赋权,新建立的表,readonly用户是无法查看的,新建一张表
edbstore=> create table tb2 as select * from tb1;
SELECT 2
edbstore=> \dp
Access privileges
Schema | Name | Type | Access privileges | Column privileges | Policies
----------+------+-------+---------------------------+-------------------+----------
ota_data | tb1 | table | edbstore=arwdDxt/edbstore+| |
| | | readonly=r/edbstore | |
ota_data | tb2 | table | | |
(2 rows)
readonly用户再次查看
edbstore=> \d
List of relations
Schema | Name | Type | Owner
----------+------+-------+----------
ota_data | tb1 | table | edbstore
ota_data | tb2 | table | edbstore
(2 rows) edbstore=> select * from tb2;
ERROR: permission denied for relation tb2
为了让readonly用户可以有权限继续查看新建的表,需要为该新建的schema指定default权限
edbstore=> ALTER DEFAULT PRIVILEGES IN SCHEMA ota_data GRANT SELECT ON TABLES TO readonly;
ALTER DEFAULT PRIVILEGES
edbstore=> \ddp
Default access privileges
Owner | Schema | Type | Access privileges
----------+----------+-------+---------------------
edbstore | ota_data | table | readonly=r/edbstore
(1 row) edbstore=> create table tb3 as select * from tb1;
SELECT 2
edbstore=> \dp
Access privileges
Schema | Name | Type | Access privileges | Column privileges | Policies
----------+------+-------+---------------------------+-------------------+----------
ota_data | tb1 | table | edbstore=arwdDxt/edbstore+| |
| | | readonly=r/edbstore | |
ota_data | tb2 | table | | |
ota_data | tb3 | table | edbstore=arwdDxt/edbstore+| |
| | | readonly=r/edbstore | |
(3 rows)
可以看到新建的表tb3会自动继承我们给该schema下的表预定义的select权限。
edbstore=> select * from information_schema.role_table_grants where grantee='readonly';
grantor | grantee | table_catalog | table_schema | table_name | privilege_type | is_grantable | with_hierarchy
----------+----------+---------------+--------------+------------+----------------+--------------+----------------
edbstore | readonly | edbstore | ota_data | tb1 | SELECT | NO | YES
edbstore | readonly | edbstore | ota_data | tb3 | SELECT | NO | YES
(2 rows)
如何取消该权限
alter default privileges in schema public revoke select on tables from readonly ;
关于如何对整个输几局所有schema添加权限可以参考如下命令:
select 'grant usage on schema '|| schema_name || ' to readonly ;' from information_schema.schemata; select 'GRANT SELECT ON ALL TABLES IN SCHEMA '|| schema_name || ' TO readonly ;' from information_schema.schemata; select 'ALTER DEFAULT PRIVILEGES IN SCHEMA '|| schema_name || ' GRANT SELECT ON TABLES TO readonly ;' from information_schema.schemata;
PostgreSQL创建只读账户的更多相关文章
- Mysql 创建只读账户
mysql 创建只读账户: 1.查询所有账号信息 SELECT DISTINCT a.`User`,a.`Host`,a.password_expired,a.password_last_change ...
- PostgreSQL创建只读权限的用户
1.创建只读角色 CREATE ROLE readaccess; 2.授予对现有表的访问权限 GRANT USAGE ON SCHEMA public TO readaccess; GRANT SEL ...
- PostgreSQL创建只读用户
创建用户及指定密码: CREATE USER readonly WITH ENCRYPTED PASSWORD 'ropass'; 设置用户默认事务只读: alter user readonly se ...
- K8S dashboard 创建只读账户
1.创建名字为“Dashboard-viewonly“的Cluster Role,各种资源只给予了list,get,watch的权限.dashboard-viewonly.yaml --- apiVe ...
- Azure SQL Database (25) Azure SQL Database创建只读用户
<Windows Azure Platform 系列文章目录> 本文将介绍如何在Azure SQL Database创建只读用户. 请先按照笔者之前的文章:Azure SQL Databa ...
- 如何在PostgreSQL中建只读账号
转: 如何在PostgreSQL中建只读账号 Posted on 2014-01-21 22:00:15 by osdba 在PostgreSQL中并没有CREATE TABLE权限名称,这是与其它数 ...
- ORACLE权限管理—创建只读账号
创建只读用户:grant connect to user; grant create session to user; 1.创建角色 CREATE ROLE SELECT_ROLE 2.给角色分配权限 ...
- 给你的Kubernetes集群建一个只读账户(防止高管。。。后)
给你的Kubernetes集群建一个只读账户 需求:我们知道搭完k8s集群会创建一个默认的管理员kubernetes-admin用户该用户拥有所以权限,有一天开发或测试的同学需要登录到k8s集群了解业 ...
- postgresql 设置只读用户
postgresql 设置只读用户 ` CREATE USER readonly WITH ENCRYPTED PASSWORD 'ropass'; alter user readonly set d ...
随机推荐
- 【shell&awk】对数据从右到左隔三位来一个.
chinaunix 上看到这么一题: echo 12345678.12|sed **** 期望达到效果: 12,345,678.12 我的解法是先把字符串反转,然后根据‘.’分割,分割完成后对$2部分 ...
- 18-Node.js学习笔记-Express-请求处理-构建模块化路由
构建模块化路由 const express = require('express') //创建路由对象 const home = express.Router(); //将路由和请求路径进行匹配 ap ...
- json从后台接收时转化格式
后台传回的json格式为字符串格式 需要通过转化成json对象 方法一:$.get(设置type的属性为json) 方法二:设置response.setContentType("applic ...
- border-box与content-box的区别
㈠box-sizing 属性 ⑴box-sizing 属性允许您以特定的方式定义匹配某个区域的特定元素. ⑵语法:box-sizing: content-box|border-box|inherit; ...
- json的值键对,对象,数组,逻辑值
详细说一下有关json的相关知识: ㈠json与xml的异同 ★与 XML 相同之处 ⑴JSON 是纯文本 ⑵JSON 具有"自我描述性"(人类可读) ⑶JSON 具有层级结构(值 ...
- 排序(sort)
1.定义 排序 所谓排序,就是要整理文件中的记录,使之按关键字递增(或递减)次序排列起来.其确切定义如下: 输入:n个记录R1,R2,…,Rn,其相应的关键字分别为K1,K2,…,Kn. 输出:Ril ...
- 【POJ1011】Sticks
[题目概括] 现在有\(n\)个长度不超过\(50\)的木棍,请你把这些小木棍拼成若干根长度相同的木棍. 请你最小化拼成后的长度. [思路要点] 考虑枚举最后的长度,然后判断是否可以,这样就不需要最优 ...
- POJ 1430 Binary Stirling Numbers (第二类斯特林数、组合计数)
题目链接 http://poj.org/problem?id=1430 题解 qaq写了道水题-- 在模\(2\)意义下重写一下第二类Stirling数的递推式: \[S(n,m)=S(n-1,m-1 ...
- Linux环境下TomCat使用指定JDK的版本
服务器是web服务器,在上面安装了jdk1.7和jdk1.8.及多个tomcat应用,默认/etc/profile 配置的jdk1.7,大部分tomcat应用使用的也是jdk1.7, 但目前有一个新项 ...
- 关于MYSQL日期 字符串 时间戳互转
时间转字符串: select date_format(now(), '%Y-%m-%d'); #结果:2016-01-05 时间转时间戳: select unix_timestamp(now()); ...