禁止在公共模式中创建表

kol*_*vra 6 postgresql

Postgres 版本:9.3.16

假设我们有两个用户,luser并且editor. 我想让它luser(或任何其他非超级用户)不能在公共模式下创建任何表,除了editor. 当我postgres用户身份应用以下内容时,我实现了这一点:

postgres=> select current_user;
 current_user
--------------
 postgres
(1 row)
postgres=# REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE
postgres=# GRANT ALL ON SCHEMA public TO editor WITH GRANT OPTION;
GRANT
postgres=# SET ROLE luser;
SET
postgres=> create table public.test (uid integer);
ERROR:  permission denied for schema public
postgres=> SET ROLE editor;
SET
postgres=> create table public.test (uid integer);
CREATE TABLE
postgres=> \d+
                   List of relations
 Schema | Name | Type  | Owner  |  Size   | Description
--------+------+-------+--------+---------+-------------
 public | test | table | editor | 0 bytes |
(1 row)
Run Code Online (Sandbox Code Playgroud)

但是,当如果我尝试运行REVOKE ALL ON SCHEMA public FROM PUBLIC;作为editor用户,这不起作用(即luser 可以创建公共模式下表):

postgres=> SET ROLE postgres;
SET
postgres=# GRANT ALL ON SCHEMA public TO editor WITH GRANT OPTION;
GRANT
postgres=# SET ROLE editor;
SET
postgres=> REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE
postgres=# SET ROLE luser;
SET
postgres=> create table public.test (uid integer); # I expect this to fail with `ERROR:  permission denied for schema public` but it works somehow!
CREATE TABLE
postgres=> \d+
                   List of relations
 Schema | Name | Type  | Owner |  Size   | Description
--------+------+-------+-------+---------+-------------
 public | test | table | luser | 0 bytes |
(1 row)
Run Code Online (Sandbox Code Playgroud)

所以,尽管我可以运行REVOKE ALL ON SCHEMA public FROM PUBLIC;editor,并没有给出任何错误,luser仍然可以创建公共模式下的表。这怎么会发生?

前期研究:

你如何撤消上的PostgreSQL 9.4用户创建表?

另请注意,执行撤销的用户/角色也需要是架构的所有者(因此您也可以进行检查)。

也许这就是原因?但我还是会期望Postgres的运行时给某种错误REVOKE ALL ON SCHEMA public FROM PUBLIC;editor(它,当我尝试运行此作为luser用户)...

Luc*_*ini 4

这可能会做你想要的:

\n\n
revoke create on schema public\xc2\xb9 from public\xc2\xb2; \n
Run Code Online (Sandbox Code Playgroud)\n\n

请注意,public\xc2\xb2 并不意味着模式 public\xc2\xb9,而是数据库所有用户的组表示。因此,当您尝试撤销用户创建表时,不会发生任何事情,因为它可能在开始时没有此权限(除非您授予它),但您的用户所属的组 public\xc2\xb2 默认情况下具有此访问权限- 这是模式公共的默认行为。

\n\n

现在您可以为特定用户授予创建权限:

\n\n
grant create on schema public to specificuser; \n
Run Code Online (Sandbox Code Playgroud)\n