Create or replace trigger postgres
No way to create or replace a trigger but can do this way
DROP TRIGGER IF EXISTS yourtrigger_name on "yourschemaname"."yourtablename";
Postgresql has transaction DDL so BEGIN > DROP > CREATE > COMMIT
is the equivalent of CREATE OR REPLACE
This is a nice write-up of how postgre's transactional DDL compares to other systems (such as oracle)
Current postgres planned features regarding triggers do not include adding the REPLACE
syntax.
You should use two statements: one for drop trigger and another for creating a trigger.
Example:
DROP TRIGGER IF EXISTS my_trigger
ON my_schema.my_table;
CREATE TRIGGER my_trigger
BEFORE INSERT OR UPDATE
ON my_schema.my_table
FOR EACH ROW EXECUTE PROCEDURE my_schema.my_function();
As of PostgreSQL 14, CREATE TRIGGER
now also supports "OR REPLACE
".
You can now use CREATE OR REPLACE TRIGGER ...
(instead of using DROP TRIGGER IF EXISTS
first).
This also seems to handle the case of partitioned tables sensibly:
Creating a row-level trigger on a partitioned table will cause an identical “clone” trigger to be created on each of its existing partitions; and any partitions created or attached later will have an identical trigger, too. If there is a conflictingly-named trigger on a child partition already, an error occurs unless CREATE OR REPLACE TRIGGER is used, in which case that trigger is replaced with a clone trigger. When a partition is detached from its parent, its clone triggers are removed.
Also noteworthy:
Currently, the OR REPLACE option is not supported for constraint triggers.