Sub topic 4
Time zone names, abbreviations, and offsets
This chapter assumes you know that timestamptz stores an instant (covered in the previous chapter) and that you can run queries in psql. Here you will learn the three ways PostgreSQL accepts a time zone, which one to choose, and the mistakes that follow from picking the wrong one.
Three ways to name a time zone
Anywhere PostgreSQL asks for a time zone, you can supply one of these:
| Form | Example | Offset behavior |
|---|---|---|
| Full name from the IANA tz database | `America/New_York`, `Asia/Tokyo` | Follows the region's rules, including daylight saving time (DST) and historical changes |
| Abbreviation | `EST`, `PST`, `CET` | A fixed offset from UTC, never adjusted for DST |
| Fixed offset | `+05:30` in a literal, or an `interval` value | One constant offset |
The tz database (also called the Olson or IANA database) is a record of every region's UTC offset and DST rules over time. PostgreSQL ships with its own copy, or uses the operating system's copy, depending on how it was built.
Listing the names PostgreSQL knows
Two system views show what the server accepts:
pg_timezone_nameslists full names. Its columns arename,abbrev,utc_offset, andis_dst.pg_timezone_abbrevslists abbreviations. Its columns areabbrev,utc_offset, andis_dst.
SELECT name, abbrev, utc_offset, is_dst
FROM pg_timezone_names
WHERE name IN ('America/New_York', 'Asia/Kolkata', 'Europe/Paris')
ORDER BY name;The abbrev, utc_offset, and is_dst values describe the zone at the moment you run the query. In January, America/New_York shows EST, -05:00:00, and false. In July it shows EDT, -04:00:00, and true. A zone has no single offset. The offset is a function of the instant.
Use pg_timezone_names to validate user-supplied zone names, or to populate a drop-down in an application.
Why full names are the right default
A full name carries rules, and an abbreviation carries only one number. The next query converts the same instant, noon UTC, into New York time in January and July. It then does the same for the abbreviation EST.
SET TimeZone = 'UTC';
SELECT TIMESTAMPTZ '2024-01-15 12:00:00+00' AT TIME ZONE 'America/New_York' AS ny_winter,
TIMESTAMPTZ '2024-07-15 12:00:00+00' AT TIME ZONE 'America/New_York' AS ny_summer,
TIMESTAMPTZ '2024-07-15 12:00:00+00' AT TIME ZONE 'EST' AS est_in_summer;
-- ny_winter: 2024-01-15 07:00:00
-- ny_summer: 2024-07-15 08:00:00
-- est_in_summer: 2024-07-15 07:00:00In July, New York is on EDT (UTC-4), so the correct local time is 08:00. EST is permanently UTC-5, so it gives 07:00, an hour off. This is the most common time zone name bug: someone stores or passes 'EST' meaning "Eastern time" and gets the wrong answer for half of the year.
Abbreviations are ambiguous
Abbreviations are not unique across the world. CST can mean US Central Standard Time, China Standard Time, or Cuba Standard Time. IST can mean India, Israel, or Ireland. PostgreSQL resolves abbreviations using a configurable list, selected by the timezone_abbreviations setting:
SHOW timezone_abbreviations;
-- Default
SELECT abbrev, utc_offset, is_dst
FROM pg_timezone_abbrevs
WHERE abbrev IN ('EST', 'EDT', 'IST')
ORDER BY abbrev;The Default list picks one meaning for each abbreviation. That meaning might not be the one your user intended. Abbreviations are also not a stable identity. A government can change a zone's rules, and its abbreviation does not tell you which rules applied.
Abbreviations do appear legitimately in timestamp input, because people write them in logs and emails:
SET TimeZone = 'UTC';
SELECT TIMESTAMPTZ '2024-07-01 12:00:00 EST';
-- 2024-07-01 17:00:00+00PostgreSQL reads EST as UTC-5, so noon becomes 17:00 UTC. The abbreviation is used only to compute the instant. It is not stored with the value, and the session TimeZone setting decides how the value is displayed later. For output, to_char(ts, 'TZ') prints the abbreviation of the session zone at that instant.
History and future rules
A full name is a lookup into a table of past and present rules. The same name can produce different offsets for different years. Brazil abolished DST in 2019, and America/Sao_Paulo reflects this:
SET TimeZone = 'UTC';
SELECT TIMESTAMPTZ '2018-12-01 12:00:00+00' AT TIME ZONE 'America/Sao_Paulo' AS dec_2018,
TIMESTAMPTZ '2020-12-01 12:00:00+00' AT TIME ZONE 'America/Sao_Paulo' AS dec_2020;
-- dec_2018: 2018-12-01 10:00:00
-- dec_2020: 2020-12-01 09:00:00In December 2018 the offset was UTC-2 (summer time). In December 2020 it was UTC-3. A fixed offset cannot reproduce this.
Rules also change going forward, and governments sometimes announce changes with little notice. The server uses the tz data it was built or installed with. If you store a future local appointment as a timestamptz, you fix the instant. If the rules change afterward, the local wall-clock time of that instant changes too. Keep the tz data current by applying PostgreSQL minor releases and operating system updates.
Fixed offsets and the sign trap
When you only know a numeric offset, you have two options.
The first is an ISO 8601 offset inside a literal, such as '2024-06-01 09:00:00+02'. A positive offset means east of UTC, so this instant is 07:00 UTC. The second is an interval value, as in SET TIME ZONE INTERVAL '-08:00' HOUR TO MINUTE. This also follows the ISO sign convention.
The trap is the POSIX convention, used by names such as UTC+5 and by the Etc/GMT±N zones. In POSIX, a positive number means west of Greenwich, the opposite of ISO. So Etc/GMT+5 is five hours behind UTC:
SET TimeZone = 'UTC';
SELECT TIMESTAMPTZ '2024-06-01 12:00:00+00' AT TIME ZONE 'Etc/GMT+5' AS gmt_plus_5,
TIMESTAMPTZ '2024-06-01 12:00:00+00' AT TIME ZONE 'Etc/GMT-9' AS gmt_minus_9;
-- gmt_plus_5: 2024-06-01 07:00:00
-- gmt_minus_9: 2024-06-01 21:00:00Etc/GMT+5 is UTC-5, which is why 12:00 UTC becomes 07:00. Etc/GMT-9 is UTC+9. The sign is easy to get backwards, so prefer a real region name such as Asia/Tokyo when you know the region.
PostgreSQL also accepts some POSIX-style names such as EST5EDT, which describe both a standard offset and DST rules. These are legacy forms. A full region name is clearer.
Choosing a form
Use this order of preference:
- A full IANA name, when the value represents a place or a person's location.
- A fixed offset, when the data really is a fixed offset, such as a sensor that reports
+02:00and nothing else. - An abbreviation, only when parsing text that already contains one, and only after checking what your abbreviation list maps it to.
If you store a zone alongside a timestamp, store the full name in a text column and validate it against pg_timezone_names. Do not store the offset as a substitute for the zone. An offset records what was true at one instant, and it cannot tell you what the offset will be next month.
Errors you will see
An unknown name raises an error, and the message differs by where you used it:
SET TimeZone = 'Mars/Olympus';
-- ERROR: invalid value for parameter "TimeZone": "Mars/Olympus"
SELECT now() AT TIME ZONE 'Mars/Olympus';
-- ERROR: time zone "Mars/Olympus" not recognizedA misspelled name fails loudly, which is helpful. The quieter failures are valid names that mean something other than you intended, such as EST for "Eastern" or a flipped Etc/GMT sign. Review any query that uses a zone string built from user input or from a legacy system.
Exercise
A colleague writes ts AT TIME ZONE 'PST' to show Los Angeles local times. The report is correct in January and off by one hour in July. Why, and what should replace 'PST'?
Show answer
PST is an abbreviation for a fixed UTC-8 offset, so it ignores daylight saving time. In July, Los Angeles is on PDT (UTC-4 relative to New York is irrelevant here; the offset is UTC-7), so results are an hour too early. Replace it with the full region name 'America/Los_Angeles', which applies DST rules for each date.
Exercise
What does SELECT TIMESTAMPTZ '2024-06-01 12:00:00+00' AT TIME ZONE 'Etc/GMT+3'; return when the session TimeZone is UTC, and why?
Show answer
It returns 2024-06-01 09:00:00. Etc/GMT+3 follows the POSIX sign convention, in which a positive number means west of Greenwich. The zone is therefore UTC-3, and noon UTC becomes 09:00 local time. The result type is plain timestamp.