I am a postgres noob and would like to create a trigger function for my admins table. I am following an example that is online, have checked for similar questions and tried various online examples but i am still stuck.
I have a table admins
CREATE TABLE admins (
admin_name varchar(15) PRIMARY KEY NOT NULL,
password text NOT NULL,
telephone varchar(15) NOT NULL UNIQUE,
email varchar(30) NOT NULL UNIQUE,
added timestamp DEFAULT CURRENT_TIMESTAMP,
name varchar(30) NOT NULL,
active boolean DEFAULT true
);
And an audit table aud_admins
CREATE TABLE aud_admins (
admin_name varchar(15) PRIMARY KEY NOT NULL,
password text NOT NULL,
telephone varchar(15) NOT NULL UNIQUE,
email varchar(30) NOT NULL UNIQUE,
added timestamp DEFAULT CURRENT_TIMESTAMP,
name varchar(30) NOT NULL,
active boolean DEFAULT true,
updated timestamp DEFAULT CURRENT_TIMESTAMP,
func varchar(15) NOT NULL
);
- I'm trying to create a stored trigger function to update the aud_admins table whenever there is a change but have failed woefully
This is my trigger function code:
CREATE OR REPLACE FUNCTION public.audit_admins()
RETURNS trigger AS
$BODY$
BEGIN
IF (TG_OP = 'DELETE') THEN
INSERT INTO aud_admins (admin_name,password,telephone,email,added,name,active,func) VALUES (OLD.admin_name,OLD.password,OLD.telephone,OLD.email,OLD.added,OLD.name,OLD.active,PG_OP)
RETURN OLD;
ENDIF;
IF (TG_OP = 'INSERT') THEN
INSERT INTO aud_admins (admin_name,password,telephone,email,added,name,active,func) VALUES (NEW.admin_name,NEW.password,NEW.telephone,NEW.email,NEW.added,NEW.name,NEW.active,PG_OP)
RETURN NEW;
ENDIF;
IF (TG_OP = 'UPDATE') THEN
INSERT INTO aud_admins (admin_name,password,telephone,email,added,name,active,func) VALUES (OLD.admin_name,OLD.password,OLD.telephone,OLD.email,OLD.added,OLD.name,OLD.active,PG_OP)
RETURN NEW;
ENDIF;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;
Every time I run it I get this error
**ERROR: syntax error at or near "RETURN"
LINE 7: RETURN OLD;**
Basic runtime specs:
Postgres 9.5 on Ubuntu 16.04 with pgAdmin3
Thank you for your time.