【问题标题】:Postgres: Grant access to future tables to various usersPostgres:向各种用户授予对未来表的访问权限
【发布时间】:2020-02-04 17:11:56
【问题描述】:

关于这个主题已经有几个主题了。但是我无法完成它并不断收到“权限被拒绝”错误。我一定是做错了什么。

我的目标:我希望在我的 postgres 数据库“mydatabase”上拥有

  1. 属于角色“group_x”的一大组用户
  2. 角色“group_x”的每个成员都应该有权访问某个模式“schema_x”
  3. 在该架构中,所有组成员都应该能够创建新表并相互编辑(未来)表。

我是如何尝试的(大致):

psql -h /var/run/postgresql -p5433 -U postgres

create role group_x;

create role user_a LOGIN INHERIT PASSWORD <password>;
create role user_b LOGIN INHERIT PASSWORD <password>;

grant group_x to user_a;
grant group_x to user_b;

\connect mydatabase;

grant all on schema schema_x to group_x;
grant all on all tables in schema schema_x to group_x;
grant all on all sequences in schema schema_x to group_x;
grant all on all functions in schema schema_x to group_x;

set role group_x;

alter default privileges for role group_x in schema schema_x
    grant all on tables to group_x;
alter default privileges for role group_x in schema schema_x
    grant all on sequences to group_x;
alter default privileges for role group_x in schema schema_x
    grant all on functions to group_x;

感谢您指出我的错误!

【问题讨论】:

    标签: database postgresql permissions privileges sql-grant


    【解决方案1】:

    如果您是ALTER DEFAULT PRIVILEGES <strong>FOR ROLE group_x</strong>,则意味着权限仅授予用户group_x创建的未来对象。

    所以你需要在FOR ROLE子句中指定创建表的用户。

    【讨论】:

    • hmm,所以这对我来说很难,因为我事先不知道哪个用户将创建哪些表。我希望我能够创建一个用户组来协调权限并授予该组的所有用户访问权限。有替代品吗?向所有现实生活中的用户分发相同的用户名和密码?但是我想如果几个人同时使用相同的用户角色访问数据库会遇到麻烦?
    • 您可以运行多个ALTER DEFAULT PRIVILEGES 语句,每个用户一个。您还可以将架构上的CREATE 仅授予所有用户共有的角色,并且他们必须在创建表之前SET ROLE
    • 您的说明非常有帮助。我将尝试您的第一个建议(为每个创建的新用户运行语句)。第二个建议不适用于我的用例,因为表可能是通过 QGIS 软件接口创建的,那里没有设置(我知道)来实现角色更改。我在业余时间做这个项目,所以我可能需要几天时间来报告它的进展情况,但非常感谢你的帮助。
    • 通过将上述授权顺序从 alter default privileges for role group_x ... 更改为 alter default privileges for role user_a ... 并为所有用户重复此操作,我得到了所需的功能,正如 @laurenz-albe 所建议的那样。非常感谢。 (缺点是,我必须为每个新模式重复一遍——因为我们希望为每个项目以及每个新用户使用新模式)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 2020-02-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多