Sample Header Ad - 728x90

Postgres preventing update on "events" table but insert "warning" event

0 votes
1 answer
206 views
I'm facing a bit of a problem on some restrictions I'm trying to implement on my postgresql database. My dilemma is as follows: I have an event table that logs basically everything that happens on my app, it can't be updated or deleted directly from the app (only inserts work), but I also need to prevent anyone from updating or deleting using a manual query (when conecting from dbeaver for example), I know I can do this by revoking permissions to the table, problem is, at the same time, I need to insert into that table an event that someone tried to manually update it, and when I revoke restrictions its becoming impossible. I was creating a trigger as follows, and the restriction works but its not inserting anything into my table, could anyone help me out? --Create event on event table update attempt CREATE OR REPLACE FUNCTION protect_events_on_update() RETURNS TRIGGER AS $BODY$ DECLARE username VARCHAR; BEGIN -- Get special variable values SELECT current_user INTO username; INSERT INTO events (uuid,description) VALUES (someUUID, username || 'tried to modify the table'); RETURN NEW; END; $BODY$ language plpgsql; CREATE TRIGGER protect_events_on_update_trigg BEFORE UPDATE ON events FOR EACH row EXECUTE PROCEDURE protect_events_on_update(); REVOKE ALL PRIVILEGES ON TABLE events FROM user; GRANT INSERT ON TABLE events TO user; GRANT SELECT ON TABLE events TO user;
Asked by Omaruchan (101 rep)
Sep 14, 2021, 11:58 PM
Last activity: Jun 14, 2025, 10:03 PM