iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >PostgreSQL DBA(44) - Privileges & User Management - What You Should Know
  • 536
分享到

PostgreSQL DBA(44) - Privileges & User Management - What You Should Know

2024-04-02 19:04:59 536人浏览 泡泡鱼
摘要

本文简单介绍了postgresq

本文简单介绍了postgresql的权限和用户管理基础知识,原文详见 Postgresql Privileges & User Management - What You Should Know ,有所删减和调整.

Roles
PostgreSQL使用基于角色的权限管理系统.
PostgreSQL中的用户user和角色role是一回事,区别是在创建用户时具备了LOGIN权限而角色没有,因此以下不再提及用户均以角色描述.



testdb=# create role testrole with passWord 'test';
CREATE ROLE
testdb=# create user testuser with password 'test';
CREATE ROLE

退出psql,分别以testrole和testuser登录



testdb=# \q
[pg12@localhost ~]$ psql -U testrole -d testdb
psql: error: could not connect to server: FATAL:  role "testrole" is not permitted to log in
[pg12@localhost ~]$ psql -U testuser -d testdb
psql (12beta1)
Type "help" for help.
testdb=>

在创建角色时,以下权限是常用的选项:
SUPERUSER - 超级用户,SUPERUSER可创建新的SUPERUSER,SUPERUSER可跳过所有的权限检查.
CREATEDB - 可创建databases.
CREATEROLE - 可创建其他角色.
LOGIN - 可登录.

事实上,如果没有LOGIN权限,那么就算是SUPERUSER也登录不了



testdb=# create role user1 with password 'test'
SUPERUSER CREATEROLE NOLOGIN;
CREATE ROLE
testdb=# \q
[pg12@localhost ~]$ psql -U user1 -d testdb
psql: error: could not connect to server: FATAL:  role "user1" is not permitted to log in
[pg12@localhost ~]$

在psql下,使用\du命令可查看角色信息



testdb=# \du
                                    List of roles
 Role name  |                         Attributes                         | Member of 
------------+------------------------------------------------------------+-----------
 pg12       | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 replicator | Replication                                                | {}
 testrole   | Cannot login                                               | {}
 testuser   |                                                            | {}
 user1      | Superuser, Create role, Cannot login                       | {}
InfORMational
  (options: S = show system objects, + = additional detail)
  ...
  \du[S+] [PATTERN]      list roles
  ...

pg_hba.conf
配置服务器与客户端之间的连接,查询pg_setting视图可检索当前的hba文件在什么地方



testdb=# SELECT name, setting
testdb-# FROM pg_settings WHERE name LIKE '%hba%';
   name   |             setting             
----------+---------------------------------
 hba_file | /data/pgsql/pg12db1/pg_hba.conf
(1 row)

hba文件的条目形如以下的设置



local database user address auth-method [auth-options]

其中:
第一项是指连接方式,local是Unix-domain Sockets,host是tcp/IP连接
第二项是数据库,all表示所有
第三项是用户,all表示所有
第四项是地址,如192.168.0.0/16
第五项auth-method是认证方法,包括trust,reject,scram-sha-256,md5,password,gss,sspi,ident,peer,ldap,radius,cert,pam,bsd.详见的,trust表示不需要password,password表示明文密码,md5表示使用md5加密密码传输等

通过查询pg_hba_file_rules视图可查看当前的hba配置



testdb=# SELECT * FROM pg_hba_file_rules;
 line_number | type  |   database    | user_name |    address    |                 netmask                 | auth_method | options | error 
-------------+-------+---------------+-----------+---------------+-----------------------------------------+-------------+---------+-------
          84 | local | {all}         | {all}     |               |                                         | trust       |         | 
          86 | host  | {all}         | {all}     | 127.0.0.1     | 255.255.255.255                         | trust       |         | 
          87 | host  | {all}         | {all}     | 192.168.0.0   | 255.255.0.0                             | md5         |         | 
          89 | host  | {all}         | {all}     | ::1           | ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff | trust       |         | 
          92 | local | {replication} | {all}     |               |                                         | trust       |         | 
          93 | host  | {replication} | {all}     | 127.0.0.1     | 255.255.255.255                         | trust       |         | 
          94 | host  | {replication} | {all}     | ::1           | ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff | trust       |         | 
          95 | host  | {replication} | {all}     | 192.168.26.26 | 255.255.255.255                         | trust       |         | 
          96 | host  | {replication} | {all}     | 192.168.26.27 | 255.255.255.255                         | trust       |         | 
          97 | host  | {replication} | {all}     | 192.168.26.29 | 255.255.255.255                         | trust       |         | 
(10 rows)

修改pg_hba.conf文件后,可通过pg_ctl reload命令刷新配置文件到pg_hba_file_rules中.
比如删除line_number = 97的条目,刷新



host    replication     all             192.168.26.26/32            trust
host    replication     all             192.168.26.27/32            trust
~                                                                                                                                                                                                         
:x
[pg12@localhost pg12db1]$ pg_ctl reload
server signaled
testdb=# SELECT * FROM pg_hba_file_rules;
 line_number | type  |   database    | user_name |    address    |                 netmask                 | auth_method | options | error 
-------------+-------+---------------+-----------+---------------+-----------------------------------------+-------------+---------+-------
          84 | local | {all}         | {all}     |               |                                         | trust       |         | 
          86 | host  | {all}         | {all}     | 127.0.0.1     | 255.255.255.255                         | trust       |         | 
          87 | host  | {all}         | {all}     | 192.168.0.0   | 255.255.0.0                             | md5         |         | 
          89 | host  | {all}         | {all}     | ::1           | ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff | trust       |         | 
          92 | local | {replication} | {all}     |               |                                         | trust       |         | 
          93 | host  | {replication} | {all}     | 127.0.0.1     | 255.255.255.255                         | trust       |         | 
          94 | host  | {replication} | {all}     | ::1           | ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff | trust       |         | 
          95 | host  | {replication} | {all}     | 192.168.26.26 | 255.255.255.255                         | trust       |         | 
          96 | host  | {replication} | {all}     | 192.168.26.27 | 255.255.255.255                         | trust       |         | 
(9 rows)

Database, Table, and Column level privileges
Role一旦创建,具备LOGIN权限,并且在hba中配置可以访问数据库,那么就具备了操纵数据库的权限包括创建数据表/插入数据等DDL/DML的权限,但如果需要访问其他owner创建的对象,则需要授权.
比如用户pg12创建了数据表t1,但没有授权给demouser,虽然demouser可以访问t1,但无法查询



[pg12@localhost ~]$ psql -h 192.168.26.28 -U demouser -d testdb
Password for user demouser: 
psql (12beta1)
Type "help" for help.
testdb=> create table t2(id int);
CREATE TABLE
testdb=> drop table t2;
DROP TABLE
testdb=> \d+ t1
                                    Table "public.t1"
 Column |  Type   | Collation | Nullable | Default | Storage | Stats target | Description 
--------+---------+-----------+----------+---------+---------+--------------+-------------
 id     | integer |           |          |         | plain   |              | 
 c1     | integer |           |          |         | plain   |              | 
 c2     | integer |           |          |         | plain   |              | 
Access method: heap
testdb=> select * from t1;
psql: ERROR:  permission denied for table t1

另外,PostgreSQL为了实现精细化的权限管理,提供了列级的访问授权,其GRANT语句语法如下,其中column_name部分可指定列权限:



GRANT { { SELECT | INSERT | UPDATE | REFERENCES } ( column_name [, ...] )
[, ...] | ALL [ PRIVILEGES ] ( column_name [, ...] ) }
ON [ TABLE ] table_name [, ...]
TO role_specification [, ...] [ WITH GRANT OPTION ]

指定t1.id可以给demouser访问:



testdb=# GRANT SELECT (id) ON TABLE t1 TO demouser;
GRANT

demouser可以访问id列



testdb=> select * from t1;
psql: ERROR:  permission denied for table t1
testdb=> select id from t1;
 id 
----
(0 rows)

参考资料
PostgreSQL Privileges & User Management - What You Should Know
CREATE ROLE

您可能感兴趣的文档:

--结束END--

本文标题: PostgreSQL DBA(44) - Privileges & User Management - What You Should Know

本文链接: https://www.lsjlt.com/news/45285.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • sql中逻辑运算符的用法
    sql中的逻辑运算符组合布尔表达式,产生单个布尔值。常用的运算符包括:and:两个表达式都为真时返回真or:至少一个表达式为真时返回真not:反转表达式的真假值这些运算符用于查询、过滤和...
    99+
    2024-05-15
  • foreignkey在sql中的作用
    外键是 sql 中的约束,在表之间建立连接,以确保数据一致性和完整性。其主要作用包括:保持数据一致性、强制参照完整性、便捷级联操作和简化查询。外键通过在子表中建立一个外键列,引用父表中的...
    99+
    2024-05-15
  • regexp在sql中的用法
    regexp 运算符用于 sql 查询中的文本模式匹配。其语法为:where column_name regexp 'pattern',其中 pattern 是正则表达式模式。...
    99+
    2024-05-15
  • sql中如何插入数据
    在 sql 中插入数据,使用 insert into 语句,可通过以下步骤实现:准备 insert into 语句,指定表名和列名(可选)。使用占位符或绑定变量插入值,防止 sql 注入...
    99+
    2024-05-15
    mysql python
  • sql中如何in一个数组
    sql in 运算符用于检查一个值是否属于数组。语法:select * from table_name where field_name in (value1, va...
    99+
    2024-05-15
    排列
  • sql中having的用法
    having 子句用于过滤由 group by 子句分组后的结果集,可用于筛选聚合结果、比较聚合结果以及在子查询中嵌套使用聚合函数。 SQL 中 HAVING 子句的...
    99+
    2024-05-15
    聚合函数
  • sql中split函数用法
    sql 中的 split 函数用于将字符串拆分为子字符串,使用指定的分隔符将字符串分割成一个数组。参数包括要分割的字符串以及作为分隔符使用的分隔符。返回值是一个包含分离后子字符串的数组。...
    99+
    2024-05-15
  • sql中update的用法
    update 语句用于更新数据库表中的记录,语法为:update table_name set column_name1 = value1, ... where conditi...
    99+
    2024-05-15
  • sql中如何使用双重查询
    双重查询是一种在 sql 中嵌套查询以从多个表检索数据或执行复杂过滤操作的方法。步骤如下:1. 创建一个子查询,从第一个表中检索所需数据;2. 使用 as 为子查询指定一个别名;3. 在...
    99+
    2024-05-15
  • sql中except的用法
    except 运算符从两个查询的结果集中查找不在第一个查询结果集中的行。示例:假设我们有两个表:table1 和 table2,分别包含记录 (1, 'john') 和 (2, 'mar...
    99+
    2024-05-15
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作