PostgreSQL Cheat Sheet/Create trigger

Run a function automatically when rows change.

Section: Views, Functions, Triggers, and Extensions

Create trigger

sql
sql
CREATE TRIGGER trg_users_updated_at
BEFORE UPDATE ON public.users
FOR EACH ROW
EXECUTE FUNCTION set_updated_at();
Explanation

See summary for usage details.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Views, Functions, Triggers, and Extensions
Create table
Create a basic table with primary key and defaults.
OpenIn sheetsql1 tag match
Create identity column
Use a SQL-standard identity column instead of serial.
OpenIn sheetsql1 tag match
Add column
Add a nullable column to an existing table.
OpenIn sheetsql1 tag match
Set column default
Change a column default value.
OpenIn sheetsql1 tag match
Describe a table
Show columns, indexes, defaults, storage details, and more for a relation.
OpenIn sheetsql1 tag match
Set NOT NULL
Enforce non-nullability on a column.
OpenIn sheetsql1 tag match