Sub topic 9
Dates, Timestamps, and Time Zones
This chapter assumes you know basic SELECT statements and PostgreSQL types such as date, timestamp, and timestamptz. A timestamp is a date plus a time. A time zone tells PostgreSQL how a local clock time relates to a global instant.
The three main date/time types
PostgreSQL separates calendar values from instants:
| Type | Stores | Time zone behavior |
|---|---|---|
| `date` | Calendar date | No time or zone |
| `timestamp` | Date and clock time | No time zone |
| `timestamptz` | An instant in time | Displays using the session zone |
The name timestamptz means “timestamp with time zone.” PostgreSQL does not store the original time zone name with the value. It stores an instant and converts that instant for display.
Use typed literals to make your intent clear:
SELECT DATE '2026-11-27' AS day,
TIMESTAMP '2026-11-27 10:00:00' AS local_time,
TIMESTAMPTZ '2026-11-27 10:00:00+00' AS instant;A timestamp without a time zone does not identify one unique instant. 2026-11-27 10:00:00 could refer to different instants in different regions.
Converting values and session time zones
The session TimeZone setting controls how PostgreSQL interprets and displays timestamptz values. Converting a timestamp to timestamptz therefore depends on this setting.
SET TIME ZONE 'UTC';
SELECT TIMESTAMP '2026-11-27 10:00:00'::timestamptz AS utc_value;
SET TIME ZONE 'America/New_York';
SELECT TIMESTAMP '2026-11-27 10:00:00'::timestamptz AS eastern_value;Both casts use the same wall-clock text, but they represent different instants. The first interprets the time as 10:00 in UTC. The second interprets it as 10:00 in New York.
The reverse conversion displays one instant as a local clock time:
SET TIME ZONE 'America/New_York';
SELECT TIMESTAMPTZ '2026-11-27 15:00:00+00'
AT TIME ZONE 'America/New_York' AS new_york_time;AT TIME ZONE is often clearer than relying on the session setting when a query must use a particular region.
Truncation and daylight-saving transitions
Casting a timestamp to date removes the time portion. Casting a timestamptz to date first uses the session time zone, so the result can differ between sessions.
Daylight-saving changes create local times that are either repeated or do not exist. Region names such as America/New_York contain daylight-saving rules; fixed offsets such as -05:00 do not. Prefer region names when business rules refer to a real location.
Formatting is different from casting
A plain cast parses PostgreSQL's normal input syntax. Use formatting functions when the input has a specified layout.
SELECT to_date('27/11/2026', 'DD/MM/YYYY') AS parsed_date,
to_timestamp('27/11/2026 21:30', 'DD/MM/YYYY HH24:MI') AS parsed_time;to_timestamp returns a timestamptz, so its interpretation still interacts with time-zone rules.
Exercise
What instant does TIMESTAMP '2026-11-27 10:00:00'::timestamptz represent when the session time zone is America/New_York?
Show answer
It represents 10:00 in New York, which is 15:00 UTC on that date.