在哪些网站上发外链好,重庆建设电动三轮车,网站备案要什么,网站加载速率前言#xff1a;
postgresql做为一个比较复杂的关系型的重型数据库#xff0c;不管是安装部署#xff0c;还是后期的运行维护#xff0c;都还是有比较多的细节问题需要引起关注。
例如#xff0c;用户权限的合理分配#xff0c;那么#xff0c;什么是权限的合理分配呢…前言
postgresql做为一个比较复杂的关系型的重型数据库不管是安装部署还是后期的运行维护都还是有比较多的细节问题需要引起关注。
例如用户权限的合理分配那么什么是权限的合理分配呢 自然是权限的最小化原则也就是说每个用户能够完成其权限范围内的工作而不会由于黑客攻击漏洞等原因造成安全方面的危险。
在写这篇文章之前仅仅是对于一些普通用户简单的随便赋权就完事了而这样的管理工作显然是不够的因此本文将对postgresql数据库内的用户赋权和去权做一个相对完整的总结并通过示例说明 usage权限和select权限的不同。
一
用户的权限有哪些 SELECT该权限用来查询表或是表上的某些列或是视图序列。 INSERT该权限允许对表或是视图进行插入数据操作也可以使用COPY FROM进行数据的插入。 UPDATE该权限允许对表或是或是表上特定的列或是视图进行更新操作。 DELETE该权限允许对表或是视图进行删除数据的操作。 TRUNCATE允许对表进行清空操作。 REFERENCES允许给参照列和被参照列上创建外键约束。 TRIGGER允许在表上创建触发器。 CREATE对于数据库允许在数据库上创建Schema对于Schema允许对Schema上创建数据库对象对于表空间允许把表或是索引指定到对应的表空间上。 CONNECT允许用户连接到指定的数据库上。 TEMPORARY或是TEMP允许在指定数据库的时候创建临时表。 EXECUTE允许执行某个函数。 USAGE对于程序语言来说允许使用指定的程序语言创建函数对于Schema来说允许查找该Schema下的对象不包括授权后的新建对象对于序列来说允许使用currval和nextval函数对于外部封装器来说允许使用外部封装器来创建外部服务器对于外部服务器来说允许创建外部表。 ALL PRIVILEGES表示一次性给予可以授予的权限。 OK增删改查也就是select updateinsertdelete 和usage应该是可以归于一类的而select和usage是十分相似的至少在schema下两者是基本雷同的但需要注意的是授权后的新建对象比如新建表usage是无权查询的而select显然是不存在此类问题的。
二
正确的只读用户赋权
1
第一种赋权
usage---使用权select查询权
先创建相关schema名为mytest相关role名为test
test# \c test
You are now connected to database test as user postgres.
test# create schema mytest;
CREATE SCHEMA
test# \duList of rolesRole name | Attributes | Member of
-------------------------------------------------------------------------------------------drmc | | {}pg1 | Superuser, Create role, Create DB, Replication, Bypass RLS | {}pms30 | Superuser, Create role, Create DB, Replication, Bypass RLS | {}postgres | Superuser | {}postgres_exporter | | {}postgres_exporter1 | | {}power_common | | {}power_tf | | {}zsk | | {}
test# create user test with password 123456;
CREATE ROLE赋权
test# grant USAGE on SCHEMA mytest to test;
GRANT
test1 grant SELECT on ALL tables in schema mytest to test;测试就不演示了只是需要注意一点要赋权两个usage和select两者缺一不可也就是说必须是两个命令
OK以上是用户test赋权select到test数据库下的mytest这个schema下面为了继续测试删除test这个用户。
2
强制删除已赋权过的用户
OK删除的时候报错了这就让人比较无语了报错说的是名为test的数据库有5个对象依赖于用户test不过还是有解决办法的
postgres# drop user test;
2023-08-09 01:15:34.031 CST [14975] ERROR: role test cannot be dropped because some objects depend on it
2023-08-09 01:15:34.031 CST [14975] DETAIL: 5 objects in database test
2023-08-09 01:15:34.031 CST [14975] STATEMENT: drop user test;
ERROR: role test cannot be dropped because some objects depend on it
DETAIL: 5 objects in database test
强制删除
需要reassign和drop owner by以及drop user 三条命令缺一不可。
postgres# \c test
You are now connected to database test as user postgres.
test# \dn
List of schemasName | Owner
---------------mytest | testpublic | pg1
(2 rows)test# REASSIGN OWNED BY test TO postgres;
REASSIGN OWNED
test# \dnList of schemasName | Owner
------------------mytest | postgrespublic | pg1
(2 rows)
test# drop owned BY test cascade;
NOTICE: drop cascades to 4 other objects
DETAIL: drop cascades to table mytest.dept
drop cascades to table mytest.emp
drop cascades to table mytest.bonus
drop cascades to table mytest.salgrade
DROP OWNEDOK查询test这个用户是否删除
可以看到确实没有了有强迫症的人士就非常舒服了。
但特别需要注意该强制删除用户因为是级联删除因此很大概率会把依赖的schema和table都删除所以此方式强制删除用户需要提前备份防止发生不测。
test1# \duList of rolesRole name | Attributes | Member of | Description
--------------------------------------------------------------------------------------------------------drmc | | {} | pg1 | Superuser, Create role, Create DB, Replication, Bypass RLS | {} | pms30 | Superuser, Create role, Create DB, Replication, Bypass RLS | {} | postgres | Superuser | {} | postgres_exporter | | {} | postgres_exporter1 | | {} | power_common | | {} | power_tf | | {} | zsk | | {} | 3
第二种赋权
grant select owner
test# create user test with password 123456;
CREATE ROLE
test# \c
You are now connected to database test as user postgres.
test# grant SELECT on ALL tables in schema mytest to test;
GRANT
test# set search_path to mytest ;
SET
test# alter schema mytest owner to test;
ALTER SCHEMA测试
test \c
You are now connected to database test as user test.test set search_path to mytest ;
SET
test \dpAccess privilegesSchema | Name | Type | Access privileges | Column privileges | Policies
---------------------------------------------------------------------------------mytest | bonus | table | postgresarwdDxt/postgres| | | | | testr/postgres | | mytest | dept | table | postgresarwdDxt/postgres| | | | | testr/postgres | | mytest | emp | table | postgresarwdDxt/postgres| | | | | testr/postgres | | mytest | salgrade | table | postgresarwdDxt/postgres| | | | | testr/postgres | |
(4 rows)test \dn
List of schemasName | Owner
---------------mytest | testpublic | pg1
(2 rows)test set search_path to mytest ;
SET
test select * from emp;empno | ename | job | mgr | hiredate | sal | comm | deptno
----------------------------------------------------------------------7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | | 207499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 307521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 307566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | | 207654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 307698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | | 307782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | | 107788 | SCOTT | ANALYST | 7566 | 0087-04-19 | 3000.00 | | 207839 | KING | PRESIDENT | | 1981-11-17 | 5000.00 | | 107844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 307876 | ADAMS | CLERK | 7788 | 0087-05-23 | 1100.00 | | 207900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | | 307902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | | 207934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | | 10
(14 rows)