DEV Community

Cover image for timestamp vs timestamptz: The Postgres Bug That Shifts Your Times
Muiduzzaman Lipu for Refactorly

Posted on

timestamp vs timestamptz: The Postgres Bug That Shifts Your Times

Two app servers saved the same event at the same moment. In the database they're six hours apart. No error, no failed test. The cause is one column type. Everything below was run on PostgreSQL 19 (the behavior is the same on older versions).

Video version:

The bug

CREATE TABLE events (
  id    int,
  at_ts timestamp,      -- timestamp WITHOUT time zone
  at_tz timestamptz     -- timestamp WITH time zone
);

SET timezone = 'UTC';               -- server A
INSERT INTO events VALUES (1, now(), now());

SET timezone = 'America/Chicago';   -- server B
INSERT INTO events VALUES (2, now(), now());

SET timezone = 'UTC';
SELECT * FROM events;
Enter fullscreen mode Exit fullscreen mode
 id |        at_ts        |         at_tz
----+---------------------+------------------------
  1 | 2026-11-25 09:59:53 | 2026-11-25 09:59:53+00
  2 | 2026-11-25 03:59:53 | 2026-11-25 09:59:53+00
Enter fullscreen mode Exit fullscreen mode

(Fractional seconds trimmed.) The timestamptz column is right twice. The timestamp column stored each server's local clock time: six hours apart for the same instant.

In real life, "server B" is a worker container with a different TZ, a developer laptop running a script against production, or a connection pool that sets a session time zone.

Why

timestamp (without time zone) is a wall-clock reading, like a time written on a sticky note. It doesn't know which zone it belongs to. If you give it an offset, Postgres silently throws it away:

SELECT '2026-11-25 12:00:00+00'::timestamp;
-- 2026-11-25 12:00:00
Enter fullscreen mode Exit fullscreen mode

timestamptz stores an absolute point in time (internally UTC) and converts it to the session's TimeZone on output:

SET timezone = 'America/Chicago';
SELECT '2026-11-25 12:00:00+00'::timestamptz;
-- 2026-11-25 06:00:00-06

SET timezone = 'Asia/Dhaka';
SELECT '2026-11-25 12:00:00+00'::timestamptz;
-- 2026-11-25 18:00:00+06
Enter fullscreen mode Exit fullscreen mode

Same value, displayed for the reader. Despite the name, timestamptz doesn't store the zone; it stores the instant. Both types are 8 bytes.

Bonus bug: daylight saving time

On March 8, 2026, clocks in Chicago jump from 2:00 to 3:00. Between midnight and 6:00 am that day, only five real hours pass:

SET timezone = 'America/Chicago';

SELECT timestamp   '2026-03-08 06:00' - timestamp   '2026-03-08 00:00';
-- 06:00:00

SELECT timestamptz '2026-03-08 06:00' - timestamptz '2026-03-08 00:00';
-- 05:00:00
Enter fullscreen mode Exit fullscreen mode

If you bill by the hour, measure session lengths or schedule jobs, plain timestamp is off twice a year.

The fix: use timestamptz for events

CREATE TABLE events (
  created_at timestamptz NOT NULL DEFAULT now()
);
Enter fullscreen mode Exit fullscreen mode

Anything that happened at a moment (created_at, paid_at, last_login_at) should be timestamptz. The PostgreSQL wiki's "Don't Do This" page says the same.

Plain timestamp is fine for wall-clock values that really have no zone, like "the store opens at 09:00 local time" in a table that also stores the store's zone.

Migrating an existing column: the trap

CREATE TABLE posts (id int, created_at timestamp);
INSERT INTO posts VALUES (1, '2026-11-25 15:00');  -- written by an app running in UTC
Enter fullscreen mode Exit fullscreen mode

Migrating without USING, from a session whose time zone is Chicago:

SET timezone = 'America/Chicago';
ALTER TABLE posts ALTER COLUMN created_at TYPE timestamptz;

SET timezone = 'UTC';
SELECT created_at FROM posts;
-- 2026-11-25 21:00:00+00     ← was 15:00 UTC
Enter fullscreen mode Exit fullscreen mode

Postgres assumed the old values were in the session's time zone. Tell it which zone your app actually wrote:

ALTER TABLE posts
  ALTER COLUMN created_at TYPE timestamptz
  USING created_at AT TIME ZONE 'UTC';
-- 2026-11-25 15:00:00+00
Enter fullscreen mode Exit fullscreen mode

(On a big table this rewrites the whole table under a lock, so plan it like any other heavy migration.)

Cheat sheet

  • timestamp = wall-clock reading with no zone; offsets are silently dropped.
  • timestamptz = an absolute instant, shown in the session's time zone.
  • App servers with different time zones + timestamp = shifted data.
  • timestamp subtraction ignores DST changes.
  • Migrate with USING col AT TIME ZONE '<zone the app wrote>'.
  • Default for event times: timestamptz.

Which type is in your tables right now? 👇


I post one tested coding tip every day on YouTube at Refactorly. The 30-second version of this one is here.

Top comments (0)