timestamp and timestamptz
Learning Focus
Use this lesson to choose the correct timestamp type and avoid time zone ambiguity.
Overview
timestamp: date/time without time zonetimestamptz: an absolute point in time, displayed in session time zone
Rule of thumb:
- use
timestamptzfor events
Example
CREATE TABLE events (
event_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
happened_at timestamptz NOT NULL DEFAULT now()
);
Convert for display:
SELECT happened_at AT TIME ZONE 'Asia/Singapore' AS local_time
FROM events;