Sub topic 9
Dates, timestamps and time zones
This chapter assumes you know the basic cast syntaxes (CAST, :: and typed literals) and the difference between implicit, assignment and explicit casts from earlier chapters. You should also be comfortable with date, timestamp and timestamptz, and be able to run the examples in psql.
Date/time conversions are the most context-dependent casts in PostgreSQL. The result can depend on the session TimeZone, the session DateStyle, and the calendar itself. This chapter covers each of these and the errors they cause.
The types and what each one stores
| Type | Stores | Time zone aware? |
|---|---|---|
| `date` | A calendar day | No |
| `time` | A wall-clock time | No |
| `timestamp` (`timestamp without time zone`) | A calendar day plus a wall-clock time | No |
| `timestamptz` (`timestamp with time zone`) | A single instant in time | Yes, converted at input and output |
A timestamptz value is stored as a UTC instant. The zone name or offset you supply is used only to compute that instant. The session TimeZone setting then controls how the value is displayed. A timestamp value is just a wall-clock reading with no zone attached.
Most conversion surprises come from moving between these two kinds of value.
Converting text to dates and timestamps
A cast from text uses the type's input function, which parses a flexible set of formats. ISO 8601 input (YYYY-MM-DD HH:MM:SS) is always unambiguous. Input such as 01/02/2026 is not, because the DateStyle setting decides whether it means 2 January or 1 February.
SET DateStyle = 'ISO, MDY';
SELECT '01/02/2026'::date AS mdy; -- 2026-01-02
SET DateStyle = 'ISO, DMY';
SELECT '01/02/2026'::date AS dmy; -- 2026-02-01Two rules follow from this:
- Use ISO input when you control the data.
- Use
to_dateorto_timestampwith an explicit format when you don't (see the formatting chapter).
Impossible dates raise an error. '2026-02-30'::date fails with date/time field value out of range.
The input function also accepts special strings: 'infinity', '-infinity', 'epoch', 'today', 'tomorrow' and 'now'. Avoid 'now'::timestamp in a table default. The cast is evaluated once, so the default can freeze at that value. Use now() or CURRENT_TIMESTAMP instead.
Offsets are ignored by timestamp
When you cast a string that contains an offset, the target type decides what happens to it.
SET TIME ZONE 'UTC';
SELECT '2026-11-27 10:00:00+05'::timestamptz AS keeps_instant, -- 2026-11-27 05:00:00+00
'2026-11-27 10:00:00+05'::timestamp AS drops_offset; -- 2026-11-27 10:00:00With timestamptz, the offset shifts the value to the correct UTC instant. With timestamp, PostgreSQL silently discards the offset. No error or warning appears, so a column typed timestamp can hold data that has quietly lost its zone information.
Converting between timestamp and timestamptz
Both directions use the session TimeZone. These casts are the central risk in this chapter.
SET TIME ZONE 'UTC';
SELECT '2026-11-27 10:00:00'::timestamp::timestamptz AS as_utc;
-- 2026-11-27 10:00:00+00
SET TIME ZONE 'America/New_York';
SELECT '2026-11-27 10:00:00'::timestamp::timestamptz AS as_new_york;
-- 2026-11-27 10:00:00-05The same text produces two different instants, five hours apart. A query like this returns different results depending on who runs it, so it is a source of bugs that are hard to reproduce. The reverse cast, timestamptz::timestamp, has the same dependency: it returns the wall-clock reading in the session zone.
To remove the dependency, name the zone in the query with AT TIME ZONE:
SET TIME ZONE 'UTC';
SELECT TIMESTAMPTZ '2026-11-27 15:00:00+00' AT TIME ZONE 'Asia/Tokyo' AS tokyo_wall_clock,
TIMESTAMP '2026-11-27 09:00:00' AT TIME ZONE 'Asia/Tokyo' AS instant;
-- tokyo_wall_clock: 2026-11-28 00:00:00 (timestamp)
-- instant: 2026-11-27 00:00:00+00 (timestamptz)The operator flips the type, which is easy to forget:
timestamptz AT TIME ZONE zreturns atimestamp: the wall clock in zonez.timestamp AT TIME ZONE zreturns atimestamptz: the instant at which that wall clock occurred in zonez.
Cast contexts for date and time types
Widening a value is implicit. Narrowing it, which loses information, requires an assignment or explicit cast.
| Conversion | Context |
|---|---|
| `date` to `timestamp` or `timestamptz` | Implicit (midnight) |
| `timestamp` to `timestamptz` | Implicit |
| `timestamptz` to `timestamp` | Assignment |
| `timestamp` or `timestamptz` to `date` | Assignment |
| `time` to `interval` | Implicit |
| `interval` to `time` | Assignment |
You can confirm any row with the pg_cast query from the earlier chapter, filtering on 'date'::regtype or 'timestamptz'::regtype.
Because date to timestamptz is implicit, WHERE created_at >= DATE '2026-11-27' compares against midnight in the session zone. The same query can therefore select different rows in sessions with different zones.
The timestamptz to date cast also uses the session zone:
SET TIME ZONE 'UTC';
SELECT TIMESTAMPTZ '2026-11-27 23:30:00+00'::date AS utc_day; -- 2026-11-27
SET TIME ZONE 'Asia/Tokyo';
SELECT TIMESTAMPTZ '2026-11-27 23:30:00+00'::date AS tokyo_day; -- 2026-11-28For reports that group by day, set the zone explicitly. Write (created_at AT TIME ZONE 'UTC')::date instead of relying on the session.
Daylight saving time edge cases
When you convert a timestamp to timestamptz in a zone with DST, two kinds of wall-clock time are problematic:
- Nonexistent times. At the spring-forward change, clocks skip an hour.
'2026-03-08 02:30'does not exist inAmerica/New_York. PostgreSQL does not raise an error. It interprets the value using the standard-time offset, so you get 03:30 EDT. - Repeated times. At the fall-back change, an hour occurs twice. A wall-clock time in that hour maps to two instants. PostgreSQL picks one by a fixed rule and does not warn you.
If your data can contain local times near a change, store the instant (timestamptz) as soon as you can. Alternatively, store the zone name next to the local time.
Epochs, formatting and extraction
Three related conversions don't use CAST:
to_timestamp(double precision)converts Unix epoch seconds to atimestamptz.EXTRACT(EPOCH FROM ts)converts back, returning seconds since 1970-01-01 00:00:00 UTC. For atimestamp, it treats the wall clock as if it were UTC.to_timestamp(text, format)also returnstimestamptz, interpreting the parsed wall clock in the session zone.
SET TIME ZONE 'UTC';
SELECT to_timestamp(1796000000) AS from_epoch,
EXTRACT(EPOCH FROM TIMESTAMPTZ '2026-11-27 00:00:00+00') AS to_epoch;Validating input before converting
Date input fails often, because real data contains impossible dates and mixed formats. On PostgreSQL 16 and later, test first:
SELECT pg_input_is_valid('2026-02-30', 'date') AS feb30,
pg_input_is_valid('2026-02-28', 'date') AS feb28;
-- feb30: false, feb28: truepg_input_is_valid checks input as the current DateStyle would parse it. Validate under the same settings you will convert with.
Index-friendly date predicates
Wrapping a column in a cast in the WHERE clause stops a plain B-tree index on that column from being used. For a day filter on timestamptz, convert the day boundaries instead of the column:
SET TIME ZONE 'UTC';
SELECT *
FROM events
WHERE created_at >= (DATE '2026-11-27')::timestamp AT TIME ZONE 'America/New_York'
AND created_at < (DATE '2026-11-28')::timestamp AT TIME ZONE 'America/New_York';This selects one calendar day in New York and uses an index on created_at. If you need an index on the day itself, timestamptz::date cannot be indexed because it depends on the session zone. An expression index with a fixed zone can be: CREATE INDEX ON events (((created_at AT TIME ZONE 'UTC')::date)). Queries must then use the identical expression.
Exercise
A session has TimeZone set to 'America/New_York'. What does SELECT TIMESTAMP '2026-11-27 10:00:00' AT TIME ZONE 'UTC'; return, what type is it, and what would it return if you wrote TIMESTAMPTZ '2026-11-27 10:00:00+00' AT TIME ZONE 'UTC' instead?
Show answer
The first query reads the wall clock as UTC and returns a timestamptz for that instant. It displays as 2026-11-27 05:00:00-05 in the New York session. The second query returns a timestamp, 2026-11-27 10:00:00, which is the wall-clock reading of that instant in UTC. The operator flips the type, and only the timestamptz display depends on the session zone.
Exercise
Why can WHERE created_at::date = DATE '2026-11-27' use a plain index on created_at in no case, and how do you rewrite it for a timestamp column?
Show answer
The cast wraps the indexed column in an expression, so the planner cannot match it to a plain B-tree index on created_at. Rewrite it as a range on the bare column: created_at >= TIMESTAMP '2026-11-27' AND created_at < TIMESTAMP '2026-11-28'.