How to lock certain columns from being edited even though user have access to editing rights for the table in postgresql.
Note that this will lock the entire table and referring to PostgreSQL there is no table level lock that can lock exclusively a specific row. row level lock in all your SELECT that will update your row and this will prevent all those SELECT that will update a row from reading your row !
How to Lock Column in Excel? To lock a column in Excel first we need to select the column which we need to Lock. Then click right anywhere on the selected column and select the Format Cells option from the right-click menu list. Now from the Protection tab of Format Cells, check the box of LOCKED with a tick.
PostgreSQL provides various lock modes to control concurrent access to data in tables. These modes can be used for application-controlled locking in situations where MVCC does not give the desired behavior.
PostgreSQL automatically detects deadlock situations and resolves them by aborting one of the transactions involved, allowing the other (s) to complete. (Exactly which transaction will be aborted is difficult to predict and should not be relied upon.)
PostgreSQL supports Column Security (as well as Row Security)
Let's call our limited role 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
You can add a trigger that barfs if a forbidden column gets changed:
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With