开发者

postgres trigger creation

开发者 https://www.devze.com 2023-01-29 08:33 出处:网络
How do I only create a trigger if it does not exist? When I do create or r开发者_运维技巧eplace, I get a syntax error so I am looking for a way to test for the existence of a trigger.

How do I only create a trigger if it does not exist?

When I do create or r开发者_运维技巧eplace, I get a syntax error so I am looking for a way to test for the existence of a trigger.

I can always select * from pg_trigger, but I am sure there is a more suitable way.

Thanks


Postgres can conditionally drop a trigger - see the docs. Do this before creating the trigger, then it will always work.

DROP TRIGGER IF EXISTS mytrigger ON mytable;

As Jack points out in the comments, this feature has only been available since 8.2; this has been out for more than four years though, so it should be available in your version.


CREATE TRIGGER (NameOfTrigger) AFTER INSERT OR UPDATE ON (NameOfTable)   

DROP TRIGGER IF EXISTS (NameOfTrigger) ON (NameOfTable);
0

精彩评论

暂无评论...
验证码 换一张
取 消