CREATE_EVENT_TRIGGER(7)

💖 CREATE EVENT TRIGGER(7) 💖

okay bestie, so we're deep in the PostgreSQL 18.6 Documentation and this page is literally about CREATE EVENT TRIGGER — define a new event trigger. yes, the big-girl version of triggers. pull up a chair 🎀


✨ NAME ✨

CREATE_EVENT_TRIGGER — define a new event trigger 👑

✨ SYNOPSIS ✨

CREATE EVENT TRIGGER name
    ON event
    [ WHEN filter_variable IN (filter_value [, ... ]) [ AND ... ] ]
    EXECUTE { FUNCTION | PROCEDURE } function_name()

✨ DESCRIPTION ✨

so basically, CREATE EVENT TRIGGER makes a brand new event trigger. whenever the event you picked happens, and the WHEN condition attached to the trigger (if there even is one) is satisfied, the trigger function runs. simple as that. 💅

for the full general intro to event triggers, go look at Chapter 38.

and the glow-up detail: whoever creates an event trigger becomes its owner. main character energy fr fr 🫶

✨ PARAMETERS ✨

  • name — the name you're giving your new trigger. it has to be unique within the database, no duplicates, we don't do that here. 🚫
  • event — the name of the event that fires a call to the function. head to Section 38.1 for the deets on event names. 📖
  • filter_variable — the name of a variable used to filter events. this is the part that lets you restrict the trigger so it only fires for a subset of the cases where it's supported. currently the only supported filter_variable is TAG. that's it. that's the whole list. 💀
  • filter_value — a list of values for the associated filter_variable that you want the trigger to fire for. for TAG, this means a list of command tags (like, for example, 'DROP FUNCTION'). 🏷️
  • function_name — a user-supplied function that's declared as taking no argument and returning type event_trigger.
okay important side note: in the syntax of CREATE EVENT TRIGGER, the keywords FUNCTION and PROCEDURE are equivalent, BUT the thing you reference has to be a function in any case, not a procedure. the PROCEDURE keyword here is historical and deprecated. so like… don't, bestie. 🙈

✨ NOTES ✨

only superusers can create event triggers. that's the vibe check. 👑

event triggers are disabled in single-user mode (see postgres(1)), and also disabled whenever event_triggers is set to false.

and listen, if some erroneous event trigger breaks your database SO badly that you can't even drop the trigger? 😭 restart with event_triggers set to false to temporarily disable event triggers, or go in via single-user mode — and then you'll be able to drop it. you're welcome. 🫶

✨ EXAMPLES ✨

want to forbid the execution of any DDL command? slay, here you go:

CREATE OR REPLACE FUNCTION abort_any_command()
  RETURNS event_trigger
 LANGUAGE plpgsql
  AS $$
BEGIN
  RAISE EXCEPTION 'command % is disabled', tg_tag;
END;
$$;

CREATE EVENT TRIGGER abort_ddl ON ddl_command_start
   EXECUTE FUNCTION abort_any_command();

✨ COMPATIBILITY ✨

there is no CREATE EVENT TRIGGER statement in the SQL standard. not a thing. we're built different. 🌈

✨ SEE ALSO ✨


and that's the whole page, bestie 💜 this was PostgreSQL 18.6, dated 2026, page CREATE EVENT TRIGGER(7) — go forth and trigger responsibly, mwah 🎀✨🦋