AI Hints
Home›PostgreSQL Tutorial›Dates and Times
PostgreSQL · Lesson 14 of 120

timestamptz and Time Zones

One moment, many clocks.

⏱ 2 min read▶ Video at 11:30🎓 Free
Watch this lesson in the PostgreSQL courseWatch on YouTube ↗Full PostgreSQL course

A review written at nine in the evening in London is mid afternoon in Toronto. Timestamp with time zone stores the exact moment, and shows it in whatever zone you ask for.

  • 9 PM in London
  • Afternoon in Toronto
  • Same moment

One moment, three zones

Set time zone changes how this session shows times. At time zone converts a moment to another zone's clock time. Run it. One review, shown in London, Toronto and Sydney time.

query.sqlsql
SET TIME ZONE 'Europe/London';

SELECT created_at,
       created_at AT TIME ZONE 'America/Toronto'  AS toronto,
       created_at AT TIME ZONE 'Australia/Sydney' AS sydney
FROM reviews
WHERE review_id = 1;
Output
SET
       created_at       |       toronto       |       sydney
------------------------+---------------------+---------------------
 2025-05-02 17:04:00+01 | 2025-05-02 12:04:00 | 2025-05-03 02:04:00
(1 row)

Time zone tips

Store moments as timestamp with time zone. Postgres stores them in UTC internally. Keep the server in UTC. And convert to the user's zone only when you show the time.

keyvalue
Storetimestamptz
Insidealways UTC
ServerUTC
Showuser's zone

Summary

Timestamp with time zone stores exact moments, and at time zone shows them on any clock. Next, intervals and rounding dates.

Key takeaways
  • timestamptz
  • AT TIME ZONE
  • UTC
Next up: Intervals and date_trunc — date maths