Sub topic 15
Guidelines for Time Zone Safe Schemas
Assume that you know basic SELECT, WHERE, GROUP BY, and CREATE TABLE, and that you can run PostgreSQL queries in psql or another SQL client. You should also know that UTC is a common reference time and that an offset describes the difference from UTC.
A time zone safe schema records the meaning of each value, not only its visible format. Choose a type according to the question your application asks.
| Meaning of the value | PostgreSQL type | Example |
|---|---|---|
| A calendar date | `date` | `2026-11-27` |
| A recurring wall-clock time | `time` | `09:30:00` |
| An instant on the global timeline | `timestamptz` | `2026-11-27 14:30:00+00` |
| A local date and time with no zone | `timestamp` | `2026-11-27 09:30:00` |
| A duration or calendar adjustment | `interval` | `2 hours`, `1 month` |
Store instants as `timestamptz`
Use timestamptz for events that happen at one instant: payments, log entries, messages, and completed jobs. PostgreSQL stores the instant independently of the session's display time zone. The session TimeZone setting controls how the value is shown.
CREATE TABLE payment (
payment_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
paid_at timestamptz NOT NULL,
amount numeric(12, 2) NOT NULL
);
INSERT INTO payment (paid_at, amount)
VALUES ('2026-11-27 09:30:00-05', 25.00);
SET TimeZone = 'UTC';
SELECT paid_at FROM payment;
-- 2026-11-27 14:30:00+00Do not use timestamp for an instant unless you also store and enforce the zone interpretation elsewhere. A plain timestamp such as 09:30 does not identify one moment. It could describe New York time, Tokyo time, or a value whose zone was accidentally discarded.
Store the zone when future local time matters
A meeting scheduled for “9:00 AM in Paris” needs more than an instant. Store the intended local timestamp and an IANA time zone name, such as Europe/Paris. The zone name contains daylight saving rules. A fixed offset such as +01 does not.
CREATE TABLE appointment (
appointment_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
local_start timestamp NOT NULL,
time_zone text NOT NULL,
CHECK (time_zone <> '')
);When the appointment becomes a real occurrence, convert it with AT TIME ZONE and store the resulting timestamptz if you need a stable instant for notifications or auditing.
SELECT local_start AT TIME ZONE time_zone AS starts_at
FROM appointment;Validate time zone names at the application boundary or against PostgreSQL's time zone catalog. Also define how the application handles nonexistent or repeated local times during daylight saving changes.
Query ranges with half-open boundaries
For a time window, include the start and exclude the end. This pattern avoids duplicate rows when adjacent windows meet and works cleanly with timestamps of any precision.
SELECT *
FROM payment
WHERE paid_at >= TIMESTAMPTZ '2026-11-01 00:00:00+00'
AND paid_at < TIMESTAMPTZ '2026-12-01 00:00:00+00';For a user's local day, first determine the local midnight boundaries, convert them to instants, then filter the timestamptz column. Do not truncate the stored value in the WHERE clause if an index can support a direct range.
Design checklist
- Use
timestamptzfor recorded instants. - Store UTC or offset-aware values at application boundaries.
- Store an IANA zone when a user's region affects future scheduling.
- Keep display formatting out of stored data.
- Use
intervalfor durations, but test month and day arithmetic around daylight saving changes. - Group by local days only after converting to the required zone.
- Use half-open ranges:
>= startand< end.
Exercise
A report must count payments made during each customer's local calendar day. The payments table has paid_at timestamptz, and the customer has an IANA zone name. Which value should be converted before grouping?
Show answer
Convert paid_at to the customer's zone with paid_at AT TIME ZONE customer_zone, then truncate or derive the local date. The stored payment instant remains a timestamptz.