Sub topic 5
Controlling the session TimeZone
This chapter assumes you know that timestamptz stores an absolute instant and that a UTC offset is the distance from UTC. Here you will learn where the TimeZone setting gets its value, how to change it, and which operations depend on it.
What the setting does
TimeZone is a per-session configuration parameter. It never changes the stored instant of a timestamptz value. It affects four things:
- Output. A
timestamptzis rendered as local wall-clock time in that zone, with the offset appended. - Input without an offset. A literal such as
'2024-06-01 09:00:00'cast totimestamptzis read as wall-clock time in the session zone. - Field functions.
extract,date_part, anddate_truncapplied to atimestamptzwork on the session-zone wall clock. - Text conversion. Casts to
textandto_charuse the session zone as well.
It has no effect on timestamp (without time zone), date, or time columns. Those hold no instant, so there is nothing to convert.
Inspecting and changing it
Use SHOW to read the current value.
SHOW TimeZone;
SELECT current_setting('TimeZone');There are three equivalent ways to change it for the current session:
SET TimeZone = 'America/New_York';
SET TIME ZONE 'America/New_York';
SELECT set_config('TimeZone', 'America/New_York', false);SET TIME ZONE is the SQL-standard spelling. The SET TimeZone form is the general configuration syntax. Parameter names are case-insensitive, so timezone works too. set_config is useful when the value comes from a variable, because you can pass it as a bound parameter instead of building a SQL string.
To go back to the value the session started with, use RESET:
RESET TimeZone;Input follows the session zone
The same text literal yields different instants depending on the setting.
SET TimeZone = 'America/New_York';
SELECT TIMESTAMPTZ '2024-06-01 09:00:00';
-- 2024-06-01 09:00:00-04
SET TimeZone = 'UTC';
SELECT TIMESTAMPTZ '2024-06-01 09:00:00';
-- 2024-06-01 09:00:00+00
SET TimeZone = 'UTC';
SELECT TIMESTAMPTZ '2024-06-01 09:00:00' = TIMESTAMPTZ '2024-06-01 09:00:00+00' AS same_instant;
-- tThe first two results are different instants. The first is 13:00 UTC and the second is 09:00 UTC. A literal that carries its own offset, such as +02, ignores the session zone on input. This is the main way a bug appears: an application inserts offset-less strings and the result depends on which connection did the insert.
Field functions follow it too
SET TimeZone = 'UTC';
SELECT extract(hour FROM TIMESTAMPTZ '2024-06-01 07:00:00+00') AS h;
-- 7
SET TimeZone = 'Asia/Tokyo';
SELECT extract(hour FROM TIMESTAMPTZ '2024-06-01 07:00:00+00') AS h;
-- 16The instant is identical in both queries. Only the wall clock used by extract changed. The same holds for date_trunc('day', some_timestamptz). It truncates to midnight in the session zone, so a report that groups by day gives different buckets in different sessions. Chapters on truncation cover how to make this explicit.
Scope: session, transaction, and persistent defaults
A plain SET lasts until the session ends or until it is changed again. SET LOCAL lasts only until the current transaction ends, and it has no effect outside a transaction block.
BEGIN;
SET LOCAL TimeZone = 'Asia/Kolkata';
SELECT TIMESTAMPTZ '2024-06-01 07:00:00+00';
-- 2024-06-01 12:30:00+05:30
COMMIT;
SHOW TimeZone;
-- back to whatever it was before BEGINset_config('TimeZone', 'Asia/Kolkata', true) is the transaction-local form. The third argument true means local.
You can also store a default so that new sessions start with it:
ALTER DATABASE appdb SET TimeZone = 'UTC';
ALTER ROLE reporting SET TimeZone = 'Europe/Berlin';
ALTER ROLE reporting IN DATABASE appdb SET TimeZone = 'Europe/London';These take effect for new connections only. Existing sessions keep the value they already have.
Where the starting value comes from
When a session starts, the value is resolved from several layers, and the more specific layer wins.
The initdb default is chosen from the server's operating-system environment when the cluster is created. A cluster built on a machine set to a local zone will therefore have that zone in postgresql.conf unless someone changed it. libpq-based clients, including psql, read the PGTZ environment variable and send it at connection time. This is why the same query can print different times from your laptop and from a server shell.
Valid values
PostgreSQL accepts several spellings, but they are not equally safe.
| Value style | Example | Notes |
|---|---|---|
| IANA zone name | `'America/New_York'` | Follows historical and future DST rules. Preferred. |
| Abbreviation | `'EST'` | Fixed offset, no DST. Not region-aware. |
| POSIX-style string | `'EST5EDT'` | Digits count hours west of Greenwich, the opposite of ISO 8601. |
| Interval or number in `SET TIME ZONE` | `SET TIME ZONE -5` | Fixed offset, with east positive. |
An unknown name is rejected immediately:
SET TimeZone = 'Mars/Base';
-- ERROR: invalid value for parameter "TimeZone": "Mars/Base"List the names the server knows from pg_timezone_names:
SELECT name, abbrev, utc_offset, is_dst
FROM pg_timezone_names
WHERE name LIKE 'Europe/P%'
ORDER BY name;The abbrev, utc_offset, and is_dst columns show the values in effect right now, so they change as DST starts and ends. Do not read them as the zone's permanent properties.
The sign conventions differ between forms. The ISO style used by SET TIME ZONE -5 and by offsets inside literals counts east as positive. POSIX strings such as 'UTC+3' count west as positive. If you must use a fixed offset, test it with a known instant before relying on it.
Connection pools and shared sessions
A session-level SET stays on the connection after your code finishes with it. With a connection pool, the next borrower inherits it. With a transaction-pooling proxy, successive transactions of one client may run on different connections, so a plain SET may not apply to the next transaction at all.
Two patterns avoid this:
- Set the zone to a fixed value in the role or database default, and convert explicitly in queries with
AT TIME ZONE. - If a request really needs a per-user zone, issue
SET LOCALat the start of each transaction.
Checking what a session will do
Before debugging a time zone issue, collect these facts from the session that misbehaves:
SELECT current_setting('TimeZone') AS session_zone,
now() AS now_tz,
now()::text AS now_text,
pg_typeof(now()) AS type;Then compare the result with another session. If the instants match and only the displayed offsets differ, the data is fine and the sessions simply use different zones. If the instants differ, an offset-less literal was probably interpreted under different settings at insert time.
Exercise
A table has starts_at timestamptz. Session A runs SET TimeZone = 'UTC' and inserts '2024-06-01 09:00:00'. Session B runs SET TimeZone = 'Asia/Tokyo' and inserts the same literal. Are the two stored values equal? What does Session B display for Session A's row?
Show answer
They are not equal. Session A stored 2024-06-01 09:00 UTC. Session B stored 09:00 in Tokyo, which is 2024-06-01 00:00 UTC, nine hours earlier. In Session B, A's row is displayed as 2024-06-01 18:00:00+09.
Exercise
Why is SET LOCAL TimeZone = 'Asia/Kolkata' safer than SET TimeZone = 'Asia/Kolkata' in an application that uses a connection pool?
Show answer
SET LOCAL is reverted when the transaction ends, so the pooled connection returns to its default zone and the next borrower is not affected. A plain SET persists on the connection and can leak into later requests.