即使用户有权访问postgresql中表格的编辑权限,如何锁定某些列也不会被编辑。
答案 0 :(得分:4)
PostgreSQL支持列安全性(以及行安全性)
让我们调用我们有限的角色authors
create table staff (
name text primary key,
salary decimal(19,4)
);
create role authors;
grant select, insert, delete, update(name) on table staff to authors;
set role authors;
insert into staff values ('frank', 100); -- works!
select * from staff; -- works!
update staff set name='jim'; -- works!
update staff set salary=999; -- permission denied
答案 1 :(得分:2)
如果禁止列发生更改,您可以添加barfs触发器:
CREATE OR REPLACE FUNCTION cerberus() RETURNS trigger
LANGUAGE plpgsql AS
$$BEGIN
IF NEW.forbiddencol IS DISTINCT FROM OLD.forbiddencol
AND current_user = 'luser'
THEN
RAISE EXCEPTION '"luser" must not update "forbiddencol"';
END IF;
RETURN NEW;
END;$$;
CREATE TRIGGER cerberus BEFORE UPDATE OF mytable
FOR EACH ROW EXECUTE PROCEDURE cerberus();