In the previous lessons, we learned how to build, interact with, and secure databases with Supabase. Database operations can be regarded as events that allow the consumer to query or mutate data. The mutation events include inserting, updating, or deleting data based on various conditions.
In this event-driven paradigm, it is also essential to be “aware” of these events and be able to take auxiliary actions around them. In other words, when an event happens, it is powerful to take action before or after the event. This is where database triggers come in. This lesson discusses how to build, manage, and run Postgres triggers with Supabase triggers and webhooks.
Postgres triggers
Postgres triggers are a specific procedural code that runs automatically before or after a database event.
Maintain data integrity is one of the most common use cases of Postgres triggers. As we learned in previous lessons, we can already provide some database constraints directly with a query (e.g., with check
, unique
, or foreign key
). However, in most use cases, we need to maintain more complex relationships and data integrity. Some examples include: