CREATE TRIGGER

An SQL command for creating triggers

CREATE TRIGGER is a DDL command for creating triggers.

CREATE TRIGGER was added in PostgreSQL 6.2.

Change history

Examples

Basic usage example for CREATE TRIGGER:

postgres=# CREATE TABLE foo (id INT);
CREATE TABLE

postgres=# CREATE FUNCTION foo_trigger()
             RETURNS TRIGGER
             LANGUAGE plpgsql
           AS $$
             BEGIN
               RAISE NOTICE 'executing trigger';
               IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
                 RETURN NEW;
               END IF;
           
               RETURN OLD;
             END;
           $$;
CREATE FUNCTION

postgres=# CREATE TRIGGER trg_foo_iud_01
             AFTER INSERT OR UPDATE OR DELETE
             ON foo
             FOR EACH ROW
             EXECUTE FUNCTION foo_trigger();
CREATE TRIGGER

postgres=# INSERT INTO foo VALUES (1);
NOTICE:  executing trigger
INSERT 0 1

Categories

DDL, SQL command, Trigger

See also

ALTER TRIGGER, DROP TRIGGER