PostgreSQL从列中撤销权限

7

我在我的 PostgreSQL 9.5 数据库中有一个名为 editor_users 的用户组。我的 product 表被授予了 select, insert, update 和 delete 操作权限,以供 editor_user 成员使用。

但是我想防止 id 列被更新。如何撤销用户的更新权限?

2个回答

5
您可以为每个列赋予权限。假设您有一个如下所示的表格:
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;

我有15列。REVOKE命令只需要一个命令吗?我不想为所有表编写列名。 - barteloma
1
如果您已经授予完整表的所有权限,那么我猜REVOKE UPDATE(id) ON product FROM GROUP editor_user;将不会产生任何影响。 - Max
你可以编写一个脚本,使用 SELECT * FROM information_schema.column_privileges WHERE table_name = 'product' 并更改除了 id 以外的所有列的权限。 - Max
3
在表级别授予权限,然后针对一列撤销权限并不能做到你想要的效果:列级别的操作不会影响表级别的授权。 - Max

3

你有两个选择。第一个选择是撤销表权限并授予列权限。如果您选择这种方式,那么最好使用Indoemation模式或系统目录来发现所有相关列,并通过程序创建授权语句。如果您选择这种方式,则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窗口中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接