ERROR: invalid input syntax for type timestamp with time zone in AT TIME ZONE conversion
0 reputation · 19 May 2025, 21:46 UTC
0 reputation · 19 May 2025, 21:46 UTC
Determine how PostgreSQL handles ambiguous local times during daylight‑saving transitions when using the AT TIME ZONE operator.
PostgreSQL selects the first matching offset for ambiguous timestamps, a behavior that is implementation‑defined and can change when the zoneinfo database is updated. There is no documented flag to choose the second offset.
Because the conversion is non‑deterministic across releases, applications that rely on a specific offset may produce inconsistent results in production versus development environments.
AT TIME ZONE conversions?PostgreSQL does not provide a configuration option or function to choose the second offset for an ambiguous local time during a DST fallback. The AT TIME ZONE operator (and the equivalent timezone function) always picks the first matching offset found in the zoneinfo database for the given zone.
timestamptz in UTC and converting only for presentation avoids the ambiguity.invalid input syntax for type timestamp with time zone errorAT TIME ZONE (e.g., a column, a cast, or a string literal).timestamp or timestamptz value. If it is a text literal, cast it explicitly: my_text::timestamp.'2023-11-05 01:30:00-05'::timestamptz.(my_value) AT TIME ZONE 'America/New_York' (if you started with a timestamp) or my_timestamptz AT TIME ZONE 'UTC' to get a wall‑clock time.SELECT '2024-01-01 12:00:00'::timestamp AT TIME ZONE 'UTC'; should return a timestamptz without error.timestamptz (UTC) and only convert to local zone for display.pg_timezone_names or the OS package) and test upgrades in a staging environment.timestamptz values or keep the original string and the chosen offset alongside it.Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 20 May 2025, 02:59 UTC
When a wall‑clock value can map to two offsets (e.g., 2023‑11‑05 01:30:00 in America/New_York), PostgreSQL picks the first matching offset by default. To force the desired one, attach the UTC offset to the literal before the conversion:
SELECT '2023-11-05 01:30:00-05'::timestamp AT TIME ZONE 'America/New_York';
This yields the DST‑fallback offset. Alternatively, use the timezone(zone, ts) form:
SELECT timezone('America/New_York', '2023-11-05 01:30:00-05'::timestamp);
SELECT * FROM pg_timezone_names WHERE name='America/New_York';Because tzdata updates can change the order of offsets, keeping the offset explicit in the source data (or converting from UTC) is the most reliable strategy for deterministic behavior.