(七)PostgreSQL的用户管理

server/2024/10/18 12:21:30/

PostgreSQL的用户管理

1 创建用户(角色)

CREATE USER现在是CREATE ROLE的别名。唯一的区别是,当命令的拼写为CREATE USER时,默认情况下会使用LOGIN,而当命令拼写为CREATE ROLE时会使用NOLOGIN。
官方文档:
在这里插入图片描述

创建测试用户test1:使用create user方式

--可以看到create user 命令提示的是CREATE ROLE,并且默认带有login权限
postgres=# create user test1 with password 'Dameng123';
CREATE ROLE
postgres=# select * from pg_roles where rolname like 'test%';rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcanlogin | rolreplication | rolconnlimit | rolpassword | rolvaliduntil | rolbypassrls | rolconfig |  oid  
---------+----------+------------+---------------+-------------+-------------+----------------+--------------+-------------+---------------+--------------+-----------+-------test1   | f        | t          | f             | f           | t           | f              |           -1 | ********    |               | f            |           | 16402
(1 row)
--test1用户可以正常登录postgres数据库
[pg16@test ~]$ psql -d postgres -U test1 -W
Password: 
psql (16.2)
Type "help" for help.postgres=> \conninfo
You are connected to database "postgres" as user "test1" via socket in "/tmp" at port "5777".
postgres=> 

创建测试用户test2:使用create role方式

--创建test2角色,并带登录权限。
postgres=# create role test2 with login password 'Dameng123';
CREATE ROLE
postgres=# select * from pg_roles where rolname like 'test%';rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcanlogin | rolreplication | rolconnlimit | rolpassword | rolvaliduntil | rolbypassrls | rolconfig |  oid  
---------+----------+------------+---------------+-------------+-------------+----------------+--------------+-------------+---------------+--------------+-----------+-------test1   | f        | t          | f             | f           | t           | f              |           -1 | ********    |               | f            |           | 16402test2   | f        | t          | f             | f           | t           | f              |           -1 | ********    |               | f            |           | 16403
(2 rows)postgres=# 
--test2用户(角色)可以正常登录postgres数据库
[pg16@test ~]$ psql -d postgres -U test2 -W
Password: 
psql (16.2)
Type "help" for help.postgres=> \conninfo
You are connected to database "postgres" as user "test2" via socket in "/tmp" at port "5777".
postgres=> 

视图pg_roles可以查出当前所有的角色,以及一些属性。
在这里插入图片描述在这里插入图片描述

2 授权

当前状态:
已创建测试数据库white,并创建schema yewu1,yewu1有t1,t2两张表。

2.1 授权

测试1:将yewu1.t1 的select 权限授给test1
第一步:执行 grant select on yewu1.t1 to test1;

white=# grant select on yewu1.t1 to test1;
GRANT

第二步:test1用户执行查询,发现报错

white=> select * from yewu1.t1;
ERROR:  permission denied for schema yewu1
LINE 1: select * from yewu1.t1;

第三步:执行 grant usage on schema yewu1 to test1;

white=# grant usage on schema yewu1 to test1;
GRANT

第四步:test1用户再次执行查询,可以正常访问表yewu1.t1

white=> select * from yewu1.t1;id 
----1
(1 row)

postgresql相比其它数据库,多了要授予usage这个权限,不然对应的用户访问不了这个schema。也就是:
1、先授予使用模式的权限, usage
2、再授予对模式内对象的权限,insert\delete\update\select 等

2.2 查询用户(角色)的所有权限

可以通过查看视图information_schema.table_privileges 和 information_schema.role_table_grants 来确认用户(角色)有哪些表的权限。
视图information_schema.table_privileges:
在这里插入图片描述

视图information_schema.role_table_grants:
在这里插入图片描述

–登录对应的数据库,再查询。即可看到角色所拥有的权限。

white=# select * from information_schema.role_table_grants where grantee = 'test1';grantor  | grantee | table_catalog | table_schema | table_name | privilege_type | is_grantable | with_hierarchy 
----------+---------+---------------+--------------+------------+----------------+--------------+----------------postgres | test1   | white         | yewu1        | t1         | SELECT         | NO           | YES
(1 row)white=# select * from information_schema.table_privileges where grantee = 'test1';grantor  | grantee | table_catalog | table_schema | table_name | privilege_type | is_grantable | with_hierarchy 
----------+---------+---------------+--------------+------------+----------------+--------------+----------------postgres | test1   | white         | yewu1        | t1         | SELECT         | NO           | YES

2.3 回收权限

语法和其它数据库相似,更详细的用法请查看官方文档。

white=# revoke select on yewu1.t1 from test1;
REVOKE
white=# revoke usage on schema yewu1 from test1;
REVOKE--再查询权限。test1的select权限已被回收。
white=# select * from information_schema.table_privileges where grantee = 'test1';grantor | grantee | table_catalog | table_schema | table_name | privilege_type | is_grantable | with_hierarchy 
---------+---------+---------------+--------------+------------+----------------+--------------+----------------
(0 rows)white=# select * from information_schema.role_table_grants where grantee = 'test1';grantor | grantee | table_catalog | table_schema | table_name | privilege_type | is_grantable | with_hierarchy 
---------+---------+---------------+--------------+------------+----------------+--------------+----------------
(0 rows)

3 删除用户

语法和其它数据库相似,更详细的用法请查看官方文档。

white=# drop role test1;
DROP ROLE
white=# \duList of rolesRole name |                         Attributes                         
-----------+------------------------------------------------------------postgres  | Superuser, Create role, Create DB, Replication, Bypass RLStest2     | 

谨记:心存敬畏,行有所止。


http://www.ppmy.cn/server/4656.html

相关文章

postman几种常见的请求方式

1、get请求直接拼URL形式 对于http接口,有get和post两种请求方式,当接口说明中未明确post中入参必须是json串时,均可用url方式请求 参数既可以写到URL中,也可写到参数列表中,都一样,请求时候都是拼URL 2&am…

Linux入门学习 之 基础操作指令讲解(小白必看)

股票的规律找到了,不是涨就是跌 一、Linux下基本指令 1.ls 指令 2.pwd 命令 3.cd 指令 4.touch 指令 5.mkdir 指令 6.rmdir指令 && rm 指令 7.man 指令 8.cp 指令 9.mv指令 10.cat 11.more 指令 12.less 指令 13.head 指令 14.tail 指令 15…

富格林:致用查明暗箱黑幕技巧

富格林认为,投资现货黄金对于新手投资者来说是一个很好的选择,但是在进行投资之前需要了解一些基本的技巧和策略用以查明暗箱黑幕。事实上,现货黄金市场充满着丰富的交易机会,以及并存的交易风险,因此投资者要想在这其…

C 练习实例23

题目: 打印出如下图案(菱形)。 ********* ****************程序分析: 先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重for循环,第一层控制行,第二层控…

Mac idea启动vue项目的时候报Permission denied

问题 node_modules/.bin/vue-cli-service: Permission denied 原因, 权限不足. 解决方案 chmod 777 node_modules/.bin/vue-cli-service 这里的chmod 777 是给启动服务提高权限。 注意赋权路径一定要相同 最后再重新启动命令即可

Proxyman Premium for Mac v5.1.1激活版:卓越的网络调试与分析工具

Proxyman Premium for Mac是一款功能强大的网络调试与分析工具,专为开发人员和测试人员精心打造。它集多种功能于一身,为用户提供了全面、高效的网络开发体验。 Proxyman Premium for Mac v5.1.1激活版下载 作为一款跨平台代理工具,Proxyman …

实验室三大常用仪器1---示波器的基本使用方法(笔记)

目录 示波器的作用 示波器的基础操作方法 示波器测量突变脉冲 示波器的作用 示波器能帮助我们干什么? 比如说某个电源用万用表测量是稳定的5V输出 但是用示波器一看确实波涛汹涌 这样的电源很可能回导致系统异常工作 又比如电脑和单片机进行串口通信时&#xf…

jvm-接口调用排查

问题描述 线上碰到个问题,某个接口调用时间特别长,线上调用接口直接报gateway time out 分析处理 1、先关闭该功能 (该功能是非核心功能) 2、本地起服务连环境排查,发现本地正常。并且线上其他接口正常,…