Triggers

Learn about creating and dropping triggers.

Triggers in PostgreSQL allow executing a function or block of code in response to specific events, such as an insert, update, or delete operations on a table. These triggers can enforce data integrity, perform cascading actions on related tables, and even send notifications. Triggers can’t be called or executed directly—they can only be fired in response to a specific event.

Creating a trigger

To create a trigger, the CREATE TRIGGER statement specifies the triggering event, the function or code block to execute, and optionally, a condition for when the trigger should fire. Triggers can also be modified or dropped using the ALTER TRIGGER and DROP TRIGGER statements, respectively.

Press + to interact
CREATE TRIGGER <trigger_name>
AFTER/BEFORE <event> ON <table_name>
FOR EACH ROW
EXECUTE PROCEDURE <function_name>();

Here, <trigger_name> is the trigger’s ...

Get hands-on with 1400+ tech skills courses.