← Back to PostgreSQL, page 2

postgresql-0022 · Easy fresher

What does this print when the session time zone changes, for timestamp and timestamptz values?

timestamptz stores an absolute instant (internally UTC) and displays it in the session's TimeZone, so the same value prints differently in Kolkata and London. timestamp without time zone stores the wall-clock text as given and never shifts. AT TIME ZONE converts between the two.

Full answer

Tables and rows used here:

sql
CREATE TABLE events (label text, at_tz timestamptz, at_local timestamp);
INSERT INTO events VALUES ('launch', '2026-12-04 10:00:00+05:30', '2026-12-04 10:00:00');
sql
SET TimeZone TO 'Asia/Kolkata'; SELECT at_tz::text, at_local::text FROM events;

PostgreSQL returns:

text
           at_tz           |       at_local
---------------------------+---------------------
 2026-12-04 10:00:00+05:30 | 2026-12-04 10:00:00
(1 row)
sql
SET TimeZone TO 'Europe/London'; SELECT at_tz::text, at_local::text, (at_local AT TIME ZONE 'Asia/Kolkata')::text AS local_as_kolkata FROM events;

Result on PostgreSQL 17:

text
         at_tz          |       at_local      |    local_as_kolkata
------------------------+---------------------+------------------------
 2026-12-04 04:30:00+00 | 2026-12-04 10:00:00 | 2026-12-04 04:30:00+00
(1 row)
sql
SET TimeZone TO 'UTC'; SELECT (at_tz AT TIME ZONE 'America/New_York')::text AS new_york_wall_clock FROM events;

Result:

text
 new_york_wall_clock
---------------------
 2026-12-03 23:30:00
(1 row)

In Kolkata, the timestamptz prints as 10:00 with offset +05:30; switched to London it prints as 04:30+00, the same instant. The plain timestamp prints 10:00 in both sessions because it has no zone. at_local AT TIME ZONE 'Asia/Kolkata' interprets the wall-clock value as Kolkata time and returns a timestamptz, displayed here in London time. Applied to a timestamptz, AT TIME ZONE goes the other way and returns the wall-clock time in New York, 23:30 on the previous day. Store instants as timestamptz and keep the user's zone in a separate column when needed.

sql
CREATE TABLE events (label text, at_tz timestamptz, at_local timestamp);
INSERT INTO events VALUES ('launch', '2026-12-04 10:00:00+05:30', '2026-12-04 10:00:00');

SET TimeZone TO 'Asia/Kolkata'; SELECT at_tz::text, at_local::text FROM events;

SET TimeZone TO 'Europe/London'; SELECT at_tz::text, at_local::text, (at_local AT TIME ZONE 'Asia/Kolkata')::text AS local_as_kolkata FROM events;

SET TimeZone TO 'UTC'; SELECT (at_tz AT TIME ZONE 'America/New_York')::text AS new_york_wall_clock FROM events;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • timestamptz is an instant shown in the session zone
  • timestamp is a wall-clock value
  • AT TIME ZONE converts both ways
  • Session TimeZone affects display only
  • Store the user's zone separately

Common mistakes

  • Storing instants in timestamp without time zone
  • Thinking timestamptz stores the original offset

Follow-ups

  • How do DST changes affect interval arithmetic?
  • What does now() return inside a transaction?

Tips

  • Show the same instant in two zones