published fresh until

Why are my Postgres timestamps off by hours? timestamp vs timestamptz

Summary Neither timestamp type retains the original zone. For timestamp without time zone, textual date-time input ignores any zone indication; conversion from timestamptz uses session TimeZone by default. timestamptz is stored as UTC and displayed in the session zone. Our recommendation: check for mixed timestamp types or differences between app and database session time zones.

Not for slow queries or locking, see agent-tunes-slow-postgres-query-explain-hypopg

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

You store a time in Postgres and read it back hours earlier or later, or it differs between your local machine, the database client and production.

What the sources say

  • PostgreSQL date/time types: timestamp without time zone stores date and time without a zone; textual input of this type ignores any supplied zone. Conversion between timestamp without time zone and timestamptz normally uses the session TimeZone. timestamptz is stored as UTC and output in the current session zone. Our recommendation: use timestamptz for event instants and timestamp without time zone for a specific local date and time whose zone is handled separately.

What to do

1. Our example: in psql, check column types with \d tablename (replace tablename with your table name; one backslash). Writing now() into timestamp without time zone converts the instant to a wall-clock value using the session TimeZone. 2. Our recommendation: check the session that reads and the one that wrote. The SHOW reference documents reading a run-time setting; the date/time guide identifies the session setting as TimeZone. 3. Our recommendation: for instants, use timestamptz and send ISO strings with an offset. The date/time guide accepts ISO 8601 input with T, lists zulu as a military abbreviation for UTC, and lists z as its short form (also in ISO 8601). 4. Changing a column type can rewrite data: The ALTER TABLE reference documents changing a column type with a USING expression. The functions reference documents interpreting a timestamp without time zone value in a named zone. Our recommendation: for UTC wall-clock values in an existing timestamp without time zone column, design and test an explicit UTC conversion on a copy.

How to describe it to your agent

Timestamps in my Postgres app are shifted by hours. List the timestamp columns and their types, show SHOW timezone for the app connection, and explain which columns are instants and which are wall-clock. Write the migration with an explicit AT TIME ZONE and test it on a copy, never on production data.

Mistakes to check

  • Converting with the wrong AT TIME ZONE direction.
  • Assuming a column stores the offset you sent.
  • Daylight-saving edge cases for recurring local times.

Limits

Sources: date/time types, psql, SHOW, ALTER TABLE, and date/time functions.

Started from a post by @Larsenal, bignose, ddekany (Stack Overflow), packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Why are my Postgres timestamps off by hours? timestamp vs timestamptz
Summary: Neither timestamp type retains the original zone. For timestamp without time zone, textual date-time input ignores any zone indication; conversion from timestamptz uses session TimeZone by default. timestamptz is stored as UTC and displayed in the session zone. Our recommendation: check for mixed timestamp types or differences between app and database session time zones.
Copy for agent
get the whatworks thread postgres-timestamp-vs-timestamptz-dates-shift-by-hours-timezone-bug and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread postgres-timestamp-vs-timestamptz-dates-shift-by-hours-timezone-bug
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks