AT TIME ZONE converts a timestamp between time zones without altering the stored value.
Use the AT TIME ZONE
operator. It re-interprets a timestamp as belonging to one zone and immediately converts it to another, returning either timestamp with time zone
or timestamp without time zone
depending on input type.
Store all order times in UTC, then convert when displaying to shoppers. Example: convert Orders.order_date
from UTC to the customer’s locale during SELECT, keeping the table in UTC for consistency.
Three common patterns:
timestamp_col AT TIME ZONE 'Zone'
timezone('Zone', timestamp_col)
SET TIME ZONE 'Zone'; SELECT ...
(session-wide)Each covers single-value conversion, functional style, and session default.
Join customer profile data, then apply AT TIME ZONE
. Example below shows converting to America/Chicago
using a subquery.
PostgreSQL’s time-zone database tracks DST automatically. Always use timestamp with time zone
plus IANA names like 'Europe/Berlin'
to stay DST-safe.
Use it in analytics scripts or app connections when every query in the session should share the same display zone. It alters only the returned text, not stored values.
Store in UTC, convert at the edge; use explicit column types; cache user zones; test queries around DST boundaries; avoid hard-coding abbreviations like PST
.
No. It only affects the value returned by the query.
You can, but it’s cheaper to index the raw UTC column and convert only in the SELECT list or at the application layer.
Use IANA names like 'America/Los_Angeles'
; abbreviations are ambiguous and discouraged.