Search code examples
postgresqltriggers

Create a trigger on a table that fires on UPDATE, DELETE, or INSERT


In Postgres, do you have to create a new trigger for each event, or is there a way to combine them and have the trigger fire whenever data is inserted, deleted or updated on a table?


Solution

  • Yes. The syntax is like:

    CREATE TRIGGER my_trigger
    AFTER INSERT OR UPDATE OR DELETE ON my_tbl
    FOR EACH ROW
    EXECUTE FUNCTION my_trigger_func();
    

    Before Postgres 11 the (misleading) keyword was PROCEDURE instead of FUNCTION, which is still accepted in Postgres 15 (and probably for much longer).

    There are many syntax variants. Details in the manual here and here.

    Related: