Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop ALL triggers from Postgres DB?

Is there any way to drop ALL triggers from ALL tables in Postgres? I know there's a pg_trigger table I could look at, but it doesn't look like it contains enough information for me to decipher which triggers I have added to my tables.

It also looks like Foreign Key constraints show up in the pg_trigger table, which I DO NOT want to drop. I just want to drop the user created trigger from my tables and keep the FKs.

Any suggestions?

like image 883
JamesD Avatar asked Jun 22 '10 12:06

JamesD


People also ask

How do you drop a trigger?

Use the DROP TRIGGER statement to remove a database trigger from the database. The trigger must be in your own schema or you must have the DROP ANY TRIGGER system privilege. To drop a trigger on DATABASE in another user's schema, you must also have the ADMINISTER DATABASE TRIGGER system privilege.

How do you drop a trigger in Pgadmin?

To drop a PostgreSQL trigger, we use the DROP TRIGGER statement with the following syntax: DROP TRIGGER [IF EXISTS] trigger-name ON table-name [ CASCADE | RESTRICT ];

Which command is for removing the trigger?

You can remove triggers using the Remove Physical File Trigger (RMVPFTRG) command, the SQL DROP TRIGGER statement, or System i® Navigator. Use the RMVPFTRG command to remove the association of a file and the trigger program.


1 Answers

Thanks, James.

The function from Drop ALL triggers from Postgres DB? strips only the occurrence from the first table and leaves the triggers with the same name in other tables. Here is the fixed function:

CREATE OR REPLACE FUNCTION strip_all_triggers() RETURNS text AS $$ DECLARE
    triggNameRecord RECORD;
    triggTableRecord RECORD;
BEGIN
    FOR triggNameRecord IN select distinct(trigger_name) from information_schema.triggers where trigger_schema = 'public' LOOP
        FOR triggTableRecord IN SELECT distinct(event_object_table) from information_schema.triggers where trigger_name = triggNameRecord.trigger_name LOOP
            RAISE NOTICE 'Dropping trigger: % on table: %', triggNameRecord.trigger_name, triggTableRecord.event_object_table;
            EXECUTE 'DROP TRIGGER ' || triggNameRecord.trigger_name || ' ON ' || triggTableRecord.event_object_table || ';';
        END LOOP;
    END LOOP;

    RETURN 'done';
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

select strip_all_triggers();
like image 153
smith3v Avatar answered Sep 22 '22 07:09

smith3v