PostgreSQL 撤销列的权限

PostgreSQL revoke privileges from column

我的 PostgreSQL 9.5 数据库中有一个名为 editor_users 的用户组。我的 产品 table 被授予 select, insert, update and deleteeditor_user 成员。

但我想阻止我的 id 列。没有人可以不更新 id 列。如何撤销用户的更新权限?

您可以为每一列赋予权限。假设您有如下 table:

CREATE TABLE product (
    id serial primary key,
    mytext text
);

您可以像这样向 editor_user 授予权限:

GRANT SELECT(id), INSERT(id) ON product TO editor_user;
GRANT SELECT(mytext), UPDATE(mytext), INSERT(mytext), REFERENCES(mytext) ON product TO editor_user;

这里有两种选择。第一个是撤销 table 并授予列。如果这样做,那么值得使用 Indoemation schema 或系统目录来发现所有相关列并以编程方式创建授权语句。如果你走那条路 array_aggarray_to_string 就是你的朋友。

例如你可以:

revoke all on table product from public, ...;
select 'grant insert(' || array_to_string(array_agg(attname), ', ') || ') to ... ;' 
  from pg_attribute
 where attrelid = 'product'::regclass and attnum > 0;

然后将输出复制并粘贴到 psql window。