How to Disable a Trigger in PostgreSQL

In PostgreSQL, we can manipulate the triggers. We can alter a trigger, enable and disable a trigger, and create or drop a trigger in Postgres. In this post, we will specifically learn and talk about disabling a trigger in Postgres. We can disable a trigger or multiple triggers associated/linked with a table.

How to Disable a Trigger in PostgreSQL?

When we want to disable only one trigger we will have to specify its name and if we have to disable all the triggers associated with a table we will simply use ALL. When we disable a trigger, it still exists in our database. But when the operation associated with that trigger occurs, the trigger is not fired.

We can disable triggers associated/linked with the table by using the ALTER TABLE DISABLE TRIGGER command. The basic syntax for this command is:

ALTER TABLE tab_name
DISABLE TRIGGER trig_name |  ALL;

The name of that table to which the trigger is associated is written after the ALTER TABLE statement. After DISABLE TRIGGER, the name of the trigger that needs to be deactivated/disabled is written. In case you want to deactivate/disable multiple triggers linked with the table you can use the ALL keyword.

Example:

We will disable the trigger “status_update” from table “project_status” in the query below:

ALTER TABLE project_status  
DISABLE TRIGGER status_update;

The output will ensure that the trigger has been deactivated:

img


A similar thing can be done by this query:

ALTER TABLE project_status  
DISABLE TRIGGER ALL;

The output for both the queries is same:

img


Conclusion:

In PostgreSQL, the "ALTER TABLE" with the "DISABLE TRIGGER" clause is used to disable a trigger. We need to specify the name of the trigger associated with a particular table in case of disabling one trigger. In case of disabling all the triggers related to a database table, we can simply use the ALL keyword.