【问题标题】:Postgresql database superuserPostgresql 数据库超级用户
【发布时间】:2014-03-06 13:44:00
【问题描述】:

我正在尝试保护由多个模式组成的数据库,如下所示:-

-public
-foo
-bar
-foobar

我想创建一个可以访问任何模式进行读取的用户,可以在 bar 中创建表,并且可以在 foo、bar 和 foobar 中惰性/更新/删除

我希望将用户创建为数据库超级用户,然后根据需要删除权限。

我想:-

CREATE USER test_superuser;
GRANT ALL on DATABASE test to test_superuser;

会这样做,但在这些命令之后 test_superuser 无法访问架构。

如何创建具有 postgres 超级用户权限但仅在命名数据库上的用户?

【问题讨论】:

    标签: postgresql permissions sql-grant


    【解决方案1】:

    原来这需要大量的修补才能实现:-

         CREATE ROLE test_database_superuser
         NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION VALID UNTIL '2020-03-06 00:00:00';
    
    
        CREATE ROLE test_user LOGIN
          ENCRYPTED PASSWORD 'md52b250919b406b707999fffb2b9f673fb'
          NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION VALID UNTIL '2020-03-06 00:00:00';
    
        GRANT test_database_superuser TO test_user;
    
        --DATABASE LEVEL PRIVELEGES
        GRANT ALL PRIVILEGES ON DATABASE test to test_database_superuser;
    
        --SCHEMA LEVEL
        GRANT ALL ON SCHEMA bar TO GROUP test_database_superuser;
        GRANT USAGE ON SCHEMA foo TO GROUP test_database_superuser;
        GRANT USAGE ON SCHEMA foobar TO GROUP test_database_superuser;
    
        GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA foo TO GROUP test_database_superuser;
        GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA foo TO GROUP test_database_superuser;
    
        GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA bar TO GROUP test_database_superuser;
        GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA bar TO GROUP test_database_superuser;
    
        GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA foobar TO GROUP test_database_superuser;
        GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA foobar TO GROUP test_database_superuser;
    
    
        --PUBLIC
        GRANT USAGE ON SCHEMA public TO GROUP test_database_superuser;
    

    【讨论】:

    • 为什么在 GRANT ALL ON SCHEMA 之后需要在模式 BAR 中的表/序列上授予 ALL PRIVILEGES ?
    【解决方案2】:

    允许usage of the schema

    GRANT { { CREATE | USAGE } [,...] | ALL [ PRIVILEGES ] }
        ON SCHEMA schema_name [, ...]
        TO { [ GROUP ] role_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]
    

    类似:

     GRANT USAGE ON SCHEMA your_schame TO test_superuser;
    

    顺便说一句,这不是“超级用户”,只是一个拥有很多权限的用户......

    【讨论】:

    • 数据库里面的schema很多,难道数据库级别真的不行吗
    【解决方案3】:

    这是一个例子:

    $ sudo su - postgres
    postgres@derrick:~$ createuser -P
    Enter name of role to add: web_app
    Enter password for new role: 
    Enter it again: 
    Shall the new role be a superuser? (y/n) n
    Shall the new role be allowed to create databases? (y/n) n
    Shall the new role be allowed to create more new roles? (y/n) n
    postgres@derrick:~$
    
    postgres@derrick:~$ createdb --owner web_app dbTest
    postgres@derrick:~$ logout
    

    【讨论】:

    • 这不回答上面请求的特定权限。它只是将所有权授予用户 web_app
    • 事后看来你是对的。我有问,如果你有时间。如此复杂的设置的场景是什么?如何通过网络访问数据库?
    • 这是面向网络的应用程序。我总是给出所需的最小访问权限而不是最大访问权限,这些要求代表基本设置。
    猜你喜欢
    • 1970-01-01
    • 2014-08-20
    • 2015-09-04
    • 1970-01-01
    • 2023-03-22
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多