一文全搞懂postgresql的权限

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一文全搞懂postgresql的权限相关的知识,希望对你有一定的参考价值。


权限结构图

pg中权限至上而下层次分明,首先要了解pg的逻辑架构,如下图

一文全搞懂postgresql的权限_数据库


需要注意的是在如上架构中数据库是严格分开的,这意味着不能同时使用两个不同的数据库,而模式不是严格分开的,可以一起使用和访问同一数据库的两个或多个模式中的对象。模式是逻辑层面的划分,那么表空间就是物理层面的划分,表空间只能属于某一个实例,但是可以被多个数据库使用。


权限总结概念

(1)在数据库中所有的权限都和角色(用户)挂钩,角色和用户的唯一区别在于,角色是nologin的,而用户允许login,仅此而已。而"public" 是一个特殊的角色,代表着所有人。

postgres=# create role r1;
CREATE ROLE
postgres=# create user u2;
CREATE ROLE
postgres=# \\du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
admin | Superuser |
cjr | |
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS |
r1 | Cannot login |
u2 |

psql -U r1
psql: error: FATAL: role "r1" is not permitted to log in

(2)超级用户允许操作任何对象,所以超级用户请妥善保管,普通用户只能操作自己创建的对象,因为它是对象的owner。

(3)有一些对象有赋予给public角色默认权限,所以建好之后,所有人都有这些默认权限。

(4)默认情况下,数据库在创建后,允许public角色连接,即允许任何人连接,需要revoke connect on database xxx from public之后,再显式执行grant connect on database xxx to xxx。

postgres=# revoke connect on database mydb from public;
REVOKE
postgres=# \\c mydb u2;
FATAL: permission denied for database "mydb"
DETAIL: User does not have CONNECT privilege.
Previous connection kept
postgres=# grant connect on database mydb to u2;
GRANT
postgres=# \\c mydb u2;
You are now connected to database "mydb" as user "u2".
postgres=# \\c mydb u3;
FATAL: permission denied for database "mydb"
DETAIL: User does not have CONNECT privilege.
Previous connection kept

postgres=# grant connect on database mydb to public;
GRANT
postgres=# \\c mydb u3;
You are now connected to database "mydb" as user "u3".
mydb=>

(5)默认情况下,数据库在创建后,不允许除了超级用户和owner之外的任何人在数据库中创建schema。

postgres=# create user u2;
postgres=# \\c mydb u2;
mydb=> create schema myschema;
ERROR: permission denied for database mydb

(6)默认情况下,数据库在创建后,会自动创建名为public的schema,这个schema的all权限已经赋予给public角色,即允许任何人在里面创建对象,所以为了方便安全,先revoke all吧,再按需授予权限。

mydb=# \\c mydb u2;

mydb=> create table t1111(id int);
CREATE TABLE

mydb=> create table myschema.t1(id int);
ERROR: permission denied for schema myschema
LINE 1: create table myschema.t1(id int);
^

(7)schema级别的权限,包括允许查看schema中的对象,允许在schema中创建对象。

(8)默认情况下新建的schema的权限不会赋予给public角色,因此除了超级用户和owner,任何人都没有权限查看schema中的对象或者在schema中新建对象。


通用的赋权操作

删除在PUBLIC建表的权限

revoke create on schema public from public;
postgres=> create table u2t1(id int);
ERROR: permission denied for schema public
LINE 1: create table u2t1(id int);

删除连接schema的权限

postgres=> \\c postgres postgres
You are now connected to database "postgres" as user "postgres".
postgres=# revoke all on database postgres from public;
REVOKE
postgres=# \\c postgres user2;
FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
Previous connection kept
postgres=#

赋予连接权限

postgres=# grant connect on database postgres to user2;
GRANT
postgres=# \\c postgres user2;
You are now connected to database "postgres" as user "user2".

但是没有shema权限

postgres=> \\c postgres postgres;  
You are now connected to database "postgres" as user "postgres".
postgres=# create table myschema.test(id int);
CREATE TABLE

postgres=# \\c postgres user2;
You are now connected to database "postgres" as user "user2".
postgres=> select * from myschema.test;
ERROR: permission denied for schema myschema
LINE 1: select * from myschema.test;

赋予schema权限,但没有表权限

grant usage on schema myschema to user2;
GRANT
postgres=# \\c postgres user2;
You are now connected to database "postgres" as user "user2".
postgres=> select * from myschema.test;
ERROR: permission denied for table test

赋予查询权限

 grant select on all tables in schema myschema to user2;


postgres=> select * from myschema.test;
id
----
(0 rows)

新建表无权限

postgres=# create table myschema.test2(id int);
CREATE TABLE
postgres=# insert into myschema.test2 values (1);
INSERT 0 1

select * from myschema.test2;
ERROR: permission denied for table test2

赋予通用权限(新建表也有权限)

postgres=# alter default privileges in schema myschema grant select on tables to user2;
ALTER DEFAULT PRIVILEGES



以上是关于一文全搞懂postgresql的权限的主要内容,如果未能解决你的问题,请参考以下文章

以太坊中RLP编码详解一文全搞懂

面试官常问的垃圾回收器,这次全搞懂

面试官常问的垃圾回收器,这次全搞懂

一文带你彻底搞懂Linux 文件权限管理

一文彻底搞懂ReentrantLock原理基于AQS的公平锁+非公平锁

一文彻底搞懂ReentrantLock原理基于AQS的公平锁+非公平锁