I have postgresSQL15 in my system and running trigger with liquibase sql script
Table name: test
Table name history: test_history
1:- Create you history table
--changeset praveen.singh:1
CREATE TABLE IF NOT EXISTS test_history
(
id serial primary key,
original_test_id BIGINT NOT NULL,
operation VARCHAR(10),
change_timestamp TIMESTAMP without time zone DEFAULT CURRENT_TIMESTAMP,
dummy_data VARCHAR(15) NOT NULL
);
--rollback drop table test_history;
--changeset praveen.singh:2 failOnError:true splitStatements:false
CREATE OR REPLACE FUNCTION test_history_trigger()
RETURNS TRIGGER LANGUAGE plpgsql AS $$
BEGIN
IF (TG_OP = 'UPDATE') THEN
INSERT INTO test_history (original_test_id, operation, change_timestamp, dummy_data)
VALUES (OLD.id, 'UPDATE', NOW(), OLD. dummy_data);
ELSIF (TG_OP = 'DELETE') THEN
INSERT INTO test_history (original_test_id, operation, change_timestamp, dummy_data)
VALUES (OLD.id, 'DELETE', NOW(), OLD. dummy_data);
END IF;
IF (TG_OP = 'UPDATE') THEN
RETURN NEW;
ELSIF (TG_OP = 'DELETE') THEN
RETURN OLD;
END IF;
END;
$$;
CREATE TRIGGER test_history_trigger
AFTER UPDATE OR DELETE
ON test
FOR EACH ROW
EXECUTE FUNCTION test_history_trigger();
2:- Run command liquibase
mvn liquibase:update -Dusername=<USER_NAME> -Dpassword= -Durl='jdbc:postgresql://localhost:5432/<DB_NAME>?currentSchema=<SCHEMA_NAME>'
Hope it will work for you all!!!