首页 > 解决方案 > 行级策略不适用于我的表

问题描述

我有一个表,我正在尝试对其应用策略,设置如下所示:

create role anonymous nologin;
create schema api;
create schema private;
create table private.mytable(
    id  serial primary key,
    description text default ''
);
create view api.mytable as select * from private.mytable;
insert into api.mytable (description) values ('row 1'), ('row 2');
grant usage on schema api to anonymous;
grant select on api.mytable to anonymous;
alter table private.mytable enable row level security;
create policy mytable_policy on private.mytable
    for select
    using (null);

当我将角色设置为anonymous并从中选择所有记录时mytable

set role anonymous;
select * from api.mytable;

我预计不会返回任何行,因为我在using策略中的子句中的表达式是,null但我得到了一切。
我尝试了不同的postgresql版本(9.5、9.6、10.3),但它们都有相同的行为,我在这里做错了吗?

标签: sqlpostgresqlrow-level-security

解决方案


更新

https://stackoverflow.com/a/33863371/5315974

RLS 不适用于这样的视图。您可以将 RLS 用于视图,尽管它是有限的。

你能做的是

alter view api.mytable owner to anonymous ;

推荐阅读