(七)PostgreSQL的用户管理

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            |           | 16402
 test2   | 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=# \du
                             List of roles
 Role name |                         Attributes                         
-----------+------------------------------------------------------------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS
 test2     | 

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

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值