I have a trigger on a PostgreSQL table that updates a timestamp field, but I want to get it in the right timezone. How can I set my column to always be in 'PST' by default? Here is my trigger:
ALTER TABLE coastal ADD latest_report TIMESTAMP;
ALTER TABLE coastal ALTER COLUMN latest_report
SET DEFAULT CURRENT_TIMESTAMP;
UPDATE coastal SET latest_report=CURRENT_TIMESTAMP;
CREATE OR REPLACE FUNCTION coastal_latest_report()
RETURNS TRIGGER AS '
BEGIN
NEW.latest_report = NOW();
RETURN NEW;
END;
' LANGUAGE 'plpgsql';
CREATE TRIGGER coastal_latest_report_modtime BEFORE UPDATE
ON coastal FOR EACH ROW EXECUTE PROCEDURE
coastal_latest_report();
new.latest_report := current_timestamp at time zone 'PST';. Note that using=for assignment in PL/pgSQL is deprecated (actually even undocumented). You should use:=postgresql.org/docs/current/static/…plpgsql. It's an identifier.