Skip to content

Instantly share code, notes, and snippets.

@litvil
Created February 6, 2019 14:48
Show Gist options
  • Save litvil/f7f7f32a128a64d68b82f021e489b755 to your computer and use it in GitHub Desktop.
Save litvil/f7f7f32a128a64d68b82f021e489b755 to your computer and use it in GitHub Desktop.
PostgresQL trigger for updated_at
-- create function
CREATE OR REPLACE FUNCTION trigger_set_update_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- create trigger
CREATE TRIGGER set_timestamp
BEFORE UPDATE ON thd_columns
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_update_timestamp();
-- rename trigger
ALTER TRIGGER set_timestamp ON thd_columns
RENAME TO set_update_timestamp;
-- disable trigger
ALTER TABLE employees
DISABLE TRIGGER log_last_name_changes;
-- disable all triggers
ALTER TABLE employees
DISABLE TRIGGER ALL;
-- drop trigger
DROP TRIGGER IF EXISTS set_update_timestamp ON thd_columns;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment