将INSERT限制为给定列中的特定值

  • 本文关键字:INSERT sql postgresql
  • 更新时间 :
  • 英文 :


是否有可能限制某些用户只能在某些列中插入指定的值?

例如table

test (id integer, value text)

和用户'restricted_user'只能在列id中插入1(在其他列中任意插入)

当然,使用行级别安全性:

ALTER TABLE test ENABLE ROW LEVEL SECURITY;
GRANT INSERT ON test TO restricted_user;
CREATE POLICY restr_ins ON test
FOR INSERT TO restricted_user
WITH CHECK (id = 1);

您必须为应该使用该表的其他用户添加适当的策略,否则他们无法对该表执行任何操作。

行级别安全(如Laurenz所示)是一个选项。使用VIEW和with check option是另一种解决方案:

create view restricted_test
as
select *
from test
where id = 1
with check option;

则直接禁止插入到表中:

revoke insert,update on test from restricted_user;

并且只允许插入到视图中:

grant insert,update on restricted_test to restricted_user;

最新更新