← All notes

PostgreSQL triggers: BEFORE, AFTER and INSTEAD OF

Short answer

A trigger runs a function automatically when rows are inserted, updated, deleted or truncated. First write a trigger function (no arguments, returns trigger), then attach it with CREATE TRIGGER. [S1][S2]

CREATE TRIGGER set_updated BEFORE UPDATE ON items
FOR EACH ROW EXECUTE FUNCTION touch_updated_at();

Key points

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: PostgreSQL Global Development Group, PostgreSQL: Documentation: 18: 37.1. Overview of Trigger Behavior, PostgreSQL License; PostgreSQL Global Development Group, PostgreSQL: Documentation: 18: CREATE TRIGGER, PostgreSQL License.

Cite this

Permanent ID: iil:note:vwmqqjg1

iinventedlight. 2026. "PostgreSQL triggers: BEFORE, AFTER and INSTEAD OF." iinventedlight Library, iil:note:vwmqqjg1, version of 2026-11-04. https://iinventedlight.com/library/notes/postgres-triggers/. License: CC-BY-4.0.

@misc{iil_note_vwmqqjg1,
  author = {{iinventedlight}},
  title = {{PostgreSQL triggers: BEFORE, AFTER and INSTEAD OF}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-04},
  url = {https://iinventedlight.com/library/notes/postgres-triggers/},
  note = {Permanent ID iil:note:vwmqqjg1. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.

For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/postgres-triggers.md.