PostgreSQL特权授予不可见

PostgreSQL特权授予不可见,postgresql,Postgresql,在PostgreSQL 10上,我有一个名为tn\u schema的模式和一个名为tn\u beta\u db的数据库。我想是的,尽管我必须连接到相关数据库才能看到模式: [T] jeff@nantes-4:~ $ sudo su postgres -c psql psql (10.10 (Ubuntu 10.10-0ubuntu0.18.04.1)) Type "help" for help. postgres=# \l L

在PostgreSQL 10上,我有一个名为
tn\u schema
的模式和一个名为
tn\u beta\u db
的数据库。我想是的,尽管我必须连接到相关数据库才能看到模式:

[T] jeff@nantes-4:~ $ sudo su postgres -c psql
psql (10.10 (Ubuntu 10.10-0ubuntu0.18.04.1))
Type "help" for help.

postgres=# \l
                                  List of databases
    Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
------------+----------+----------+-------------+-------------+-----------------------
 postgres   | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | 
 template0  | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres          +
            |          |          |             |             | postgres=CTc/postgres
 template1  | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres          +
            |          |          |             |             | postgres=CTc/postgres
 tn_beta_db | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | 
(4 rows)

postgres=# \dn
  List of schemas
  Name  |  Owner   
--------+----------
 public | postgres
(1 row)

postgres=# \c tn_beta_db 
You are now connected to database "tn_beta_db" as user "postgres".
tn_beta_db=# \dn
   List of schemas
   Name    |  Owner   
-----------+----------
 public    | postgres
 tn_schema | postgres
(2 rows)

tn_beta_db=# 
GRANT CREATE ON SCHEMA tn_schema TO tn_beta_migrator;
奇怪的是,我想授予一个用户在这个模式上创建表的权利,并对这些表执行任何必要的操作。所以我输入这个:

tn_beta_db=# ALTER DEFAULT PRIVILEGES IN SCHEMA tn_schema GRANT ALL ON TABLES TO tn_beta_migrator;
ALTER DEFAULT PRIVILEGES
tn_beta_db=# 
我的回答让我觉得一切都很好。但当我质疑时

tn_beta_db=# \du
                                       List of roles
    Role name     |                         Attributes                         | Member of 
------------------+------------------------------------------------------------+-----------
 postgres         | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 tn_beta_migrator |                                                            | {}
 tn_beta_reader   |                                                            | {}
 tn_beta_writer   |                                                            | {}

tn_beta_db=# 
看起来我什么都没做。事实上,当我作为用户
tn_beta_migrator
连接时,我发现我无法在
tn_模式中创建表:

[T] jeff@nantes-4:~ $ psql --username tn_beta_migrator --host localhost tn_beta_db
Password for user tn_beta_migrator: 
psql (10.10 (Ubuntu 10.10-0ubuntu0.18.04.1))
Type "help" for help.

tn_beta_db=> create table foo();
CREATE TABLE
tn_beta_db=> drop table foo;
DROP TABLE
tn_beta_db=> create table tn_schema.foo();
ERROR:  permission denied for schema tn_schema
LINE 1: create table tn_schema.foo();
                     ^
tn_beta_db=> 
我会注意到,我也尝试过这个方法,但不会改变结果:

GRANT ALL ON ALL TABLES IN SCHEMA tn_schema TO tn_beta_migrator;
对我误解的东西有什么指点吗

更新 我有三个用户,需要说明的是:一个是只读的,一个是r/w-only,另一个是具有完整表修改权限的。目标是允许用户创建的表可以由读卡器读取,也可以由读写器读写

根据反馈,我发布了以下命令:

    -- Connect to the correct db, as schemas are part of databases.
    \c tn_beta_db

    -- For future tables, these are the privileges I want to see.  Note
    -- that I need to alter the privileges for the role in question,
    -- even though I grant to the role as well.
    ALTER DEFAULT PRIVILEGES IN SCHEMA tn_schema FOR ROLE tn_beta_reader \
        GRANT SELECT ON TABLES TO tn_beta_reader;
    ALTER DEFAULT PRIVILEGES IN SCHEMA tn_schema FOR ROLE tn_beta_writer \
        GRANT SELECT,INSERT,UPDATE,DELETE ON TABLES TO tn_beta_writer;
    ALTER DEFAULT PRIVILEGES IN SCHEMA tn_schema FOR ROLE tn_beta_migrator \
        GRANT ALL ON TABLES TO tn_beta_migrator;

    -- This wasn't a problem, but to be explicit, I do want
    -- all three roles to be able to connect.
    GRANT CONNECT ON DATABASE tn_beta_db TO tn_beta_reader;
    GRANT CONNECT ON DATABASE tn_beta_db TO tn_beta_writer;
    GRANT CONNECT ON DATABASE tn_beta_db TO tn_beta_migrator;

    -- While I'm doing this at a time when there are not yet any tables
    -- in the database, it would be good to for this script to work
    -- even at some later time.  So here I grant the appropriate rights
    -- to those tables that might already exist, as ALTER DEFAULT
    -- does not change privileges for existing tables.
    GRANT SELECT ON ALL TABLES IN SCHEMA tn_schema TO tn_beta_reader;
    GRANT SELECT,INSERT,UPDATE,DELETE ON ALL TABLES IN SCHEMA tn_schema TO tn_beta_writer;
    GRANT ALL ON ALL TABLES IN SCHEMA tn_schema TO tn_beta_migrator;

    -- And finally, I note that this role is allowed to create tables.
    GRANT CREATE ON SCHEMA tn_schema TO tn_beta_migrator;
通过这些修改,tn_beta_migrator可以创建表,但不能从表中选择、插入或更新,也不能再次删除表

更新:解决方案 根据@laurenz albe的建议,我给出了一个新的最小示例。这导致了一个答案。对于那些跟随的人来说,问题是我忘记了授予使用权:

GRANT USAGE ON SCHEMA tn_schema TO tn_beta_migrator;

你误解的事情清单:

  • 不会更改任何现有对象(在您的情况下为架构)上的权限

    您需要在架构上添加以下内容:

    [T] jeff@nantes-4:~ $ sudo su postgres -c psql
    psql (10.10 (Ubuntu 10.10-0ubuntu0.18.04.1))
    Type "help" for help.
    
    postgres=# \l
                                      List of databases
        Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
    ------------+----------+----------+-------------+-------------+-----------------------
     postgres   | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | 
     template0  | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres          +
                |          |          |             |             | postgres=CTc/postgres
     template1  | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres          +
                |          |          |             |             | postgres=CTc/postgres
     tn_beta_db | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | 
    (4 rows)
    
    postgres=# \dn
      List of schemas
      Name  |  Owner   
    --------+----------
     public | postgres
    (1 row)
    
    postgres=# \c tn_beta_db 
    You are now connected to database "tn_beta_db" as user "postgres".
    tn_beta_db=# \dn
       List of schemas
       Name    |  Owner   
    -----------+----------
     public    | postgres
     tn_schema | postgres
    (2 rows)
    
    tn_beta_db=# 
    
    GRANT CREATE ON SCHEMA tn_schema TO tn_beta_migrator;
    
  • 您运行的
    alterdefaultprivileges
    语句只会影响用户
    postgres
    在schema
    tn_schema
    中创建表的权限,但您似乎希望
    tn_beta_migrator
    创建表

    您根本不需要
    alterdefaultprivileges
    ,因为创建表的用户成为表的所有者,默认情况下拥有表的所有权限

  • 您可以使用
    psql
    中的
    \ddp
    查看默认权限

  • 模式是数据库的一部分,因此需要连接到数据库以查看其模式

  • 如果希望由
    tn_beta_migrator
    创建的表在默认情况下获得某些权限,则必须为该用户定义默认权限(而不是像您那样为
    postgres
    ):


    你误解的事情清单:

  • 不会更改任何现有对象(在您的情况下为架构)上的权限

    您需要在架构上添加以下内容:

    [T] jeff@nantes-4:~ $ sudo su postgres -c psql
    psql (10.10 (Ubuntu 10.10-0ubuntu0.18.04.1))
    Type "help" for help.
    
    postgres=# \l
                                      List of databases
        Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
    ------------+----------+----------+-------------+-------------+-----------------------
     postgres   | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | 
     template0  | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres          +
                |          |          |             |             | postgres=CTc/postgres
     template1  | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres          +
                |          |          |             |             | postgres=CTc/postgres
     tn_beta_db | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | 
    (4 rows)
    
    postgres=# \dn
      List of schemas
      Name  |  Owner   
    --------+----------
     public | postgres
    (1 row)
    
    postgres=# \c tn_beta_db 
    You are now connected to database "tn_beta_db" as user "postgres".
    tn_beta_db=# \dn
       List of schemas
       Name    |  Owner   
    -----------+----------
     public    | postgres
     tn_schema | postgres
    (2 rows)
    
    tn_beta_db=# 
    
    GRANT CREATE ON SCHEMA tn_schema TO tn_beta_migrator;
    
  • 您运行的
    alterdefaultprivileges
    语句只会影响用户
    postgres
    在schema
    tn_schema
    中创建表的权限,但您似乎希望
    tn_beta_migrator
    创建表

    您根本不需要
    alterdefaultprivileges
    ,因为创建表的用户成为表的所有者,默认情况下拥有表的所有权限

  • 您可以使用
    psql
    中的
    \ddp
    查看默认权限

  • 模式是数据库的一部分,因此需要连接到数据库以查看其模式

  • 如果希望由
    tn_beta_migrator
    创建的表在默认情况下获得某些权限,则必须为该用户定义默认权限(而不是像您那样为
    postgres
    ):



    更改默认权限不会更改现有表上的任何实际权限,它只会在默认情况下在某个用户创建新表时分配权限。对,但我在任何人创建表之前执行了权限更改。然后我仍然无法创建表。
    GRANT ALL ON TABLES
    不包含创建表的权限,GRANT ON table objects用于使用该表。请参阅-表创建权限是架构对象的一部分。谢谢!这确实让我可以创建表作为tn_beta_migrator。但select、insert、update和drop仍然是禁止的。更改默认权限不会更改现有表上的任何实际权限,它只会在默认情况下在某个用户创建新表时分配权限。对,但我在任何人创建表之前执行了权限更改。然后我仍然无法创建表。
    GRANT ALL ON TABLES
    不包含创建表的权限,GRANT ON table objects用于使用该表。请参阅-表创建权限是架构对象的一部分。谢谢!这确实让我可以创建表作为tn_beta_migrator。但是选择、插入、更新和删除仍然是禁止的。谢谢,这很有帮助,还有链接。实际上,执行
    GRANT CREATE ON SCHEMA tn_SCHEMA TO tn_beta_migrator
    可以创建一个表。然后我被拒绝插入或删除它。重读特权页面表明CREATE是一个模式权限,而INSERT和DROP是表权限,因此我的alterdefault应该已经处理了这一点。(还有其他一些权限较小的用户,
    tn_beta_reader
    tn_beta_writer
    。这是更改默认权限的目的,即
    tn_beta_migrator
    创建的任何表都会被其他两个用户自动读取或r/w-able。)您必须为创建表的用户定义默认权限,而不是为
    postgres
    ,如我的答案更新中所示。谢谢!您所写的很有意义,但也许尽管进一步阅读了文档,我还是误解了一些内容。因此,我在问题的末尾提供了一个更新。这似乎是不同的t问题。也许您需要更改现有表的所有权。谢谢,这很有帮助,链接也很有用。实际上,执行
    GRANT CREATE ON SCHEMA tn_SCHEMA to tn_beta_migrator