Skip to content

Validate PostgreSQL timestamps, session time zones and DST

Validate PostgreSQL timestamps, session time zones and DST

When order logs and database records differ by several hours, operations staff may first check the system clock, then apply a uniform offset to historical fields. But the database may already hold the correct instants; the query connection may simply be displaying them in a different time zone. Updating those values without checking can turn a display issue into a data error. The following rollback-safe experiment uses a PostgreSQL temporary table on a dedicated server to verify column types, input interpretation and query boundaries without touching business tables.

1. Distinguish instants from wall-clock times

timestamp with time zone, usually called timestamptz, represents a specific instant on the timeline. It does not retain the region name supplied with the input; query output is governed by the session's TimeZone. By contrast, timestamp without time zone stores a date and a time of day. On its own, it cannot tell you which region that time belongs to.

Transaction timestamps and audit events generally need to represent instants. Daily business hours in a particular region also require a regional time zone and calendar rules. A column name containing created_at is not proof that its actual type is correct. First inspect the business table definition and the types used for application parameter binding, then record:

  • Whether input includes an explicit offset and whether the driver passes a time-zone-aware object.
  • Which time zones the write and read connections use.
  • Whether the page performs another conversion to local time.

IronSSH's data center is in Tokyo, Japan, but its location does not determine the database session's time zone. These examples apply to an existing PostgreSQL installation on Debian 12 or Ubuntu Server 24.04 LTS. After payment, retrieve the server credentials from the console and log in over SSH. Use the actual database connection configuration; do not assume an account or port.

Clock synchronization answers “What time is it now?” Time zone conversion answers “How should the same instant be displayed?” Gather evidence for each separately. Shifting historical timestamps in bulk is not a substitute for diagnosis.

2. Create an isolated fixture in one connection

Run the following commands in order within the same interactive psql connection. The temporary table belongs only to that connection and is dropped when the transaction ends. Do not split the code blocks across separate connections. With stop-on-error enabled, roll back after any execution failure, then restart from the beginning of this section.

\set ON_ERROR_STOP on
BEGIN;
SET LOCAL TIME ZONE 'UTC';

CREATE TEMP TABLE time_probe (
    id integer PRIMARY KEY,
    happened_at timestamptz NOT NULL,
    wall_time timestamp NOT NULL
) ON COMMIT DROP;

INSERT INTO time_probe VALUES
(1, TIMESTAMPTZ '2026-11-01 01:30:00-04',
    TIMESTAMP '2026-11-01 01:30:00'),
(2, TIMESTAMPTZ '2026-11-01 01:30:00-05',
    TIMESTAMP '2026-11-01 01:30:00');

SHOW TimeZone;
SELECT id, happened_at, wall_time
FROM time_probe ORDER BY id;

SELECT
    (SELECT happened_at FROM time_probe WHERE id = 2)
  - (SELECT happened_at FROM time_probe WHERE id = 1)
    AS elapsed;

The expected output shows happened_at as 05:30 and 06:30 in UTC, with an elapsed interval of one hour. Yet the two wall_time values are identical. The same clock-time text does not necessarily represent the same instant, and deduplicating strings is no substitute for deduplicating instants.

These dates are test fixtures, not operational records from this site. Both rows use explicit offsets to remove input ambiguity, so they do not depend on the connection's defaults.

3. Verify the repeated DST hour and conversion direction

When daylight saving time ends, some regions repeat a local hour. If a client submits only a local time without an offset, the database may accept it, but that does not establish that its interpretation matches the business intent. The interface should require an explicit offset, or business rules should resolve the ambiguity explicitly rather than relying on implicit inference.

SET LOCAL TIME ZONE 'America/New_York';

SELECT id, happened_at,
       extract(epoch FROM happened_at) AS epoch_seconds
FROM time_probe ORDER BY id;

SELECT id,
       happened_at AT TIME ZONE 'Asia/Tokyo' AS local_display,
       pg_typeof(happened_at AT TIME ZONE 'Asia/Tokyo')
FROM time_probe ORDER BY id;

The first query should show 01:30 twice, but with offsets of -04 and -05; the epoch values should differ by 3600 seconds. The second query produces the wall-clock time in the specified region, but its return type is timestamp without a time zone.

The direction of AT TIME ZONE depends on the input type. Converting an instant to a local wall-clock time removes its time zone semantics. Interpreting a time-zone-free value in a specified region does the reverse and produces an instant. Check both the values and pg_typeof during validation so that a converted display value is not written back as though it were UTC.

Also, passing text with an offset to a timestamp column does not make the column retain that offset. Interface parameters, driver bindings and database types must all follow the same contract for handling time.

4. Validate business queries with half-open intervals

For reporting windows, use half-open intervals: greater than or equal to the start, and less than the end. This prevents adjacent windows from counting boundary records twice. BETWEEN includes both endpoints, so it is not suitable for directly chaining consecutive reporting windows.

SET LOCAL TIME ZONE 'UTC';

SELECT id FROM time_probe
WHERE happened_at >= TIMESTAMPTZ '2026-11-01 05:00:00+00'
  AND happened_at <  TIMESTAMPTZ '2026-11-01 06:00:00+00'
ORDER BY id;

SELECT id FROM time_probe
WHERE happened_at >= TIMESTAMPTZ '2026-11-01 06:00:00+00'
  AND happened_at <  TIMESTAMPTZ '2026-11-01 07:00:00+00'
ORDER BY id;

The two windows should return only 1 and 2, respectively. You can then add records to the temporary table that fall exactly on the start and end boundaries, and confirm that an end-boundary record belongs only to the next window.

When reporting by a region's calendar day, convert local midnight at the start of that day and local midnight at the start of the next day into instants separately. Do not assume that a day always lasts a fixed twenty-four hours. Also avoid converting the entire column to text before filtering just for convenience. Prefer converting the query boundaries, comparing the original time column against that range, and checking the actual execution plan.

5. Turn the results into application regression checks

Once the database experiment passes, the application must read the same sample data through its actual driver. Logs should record at least the complete time value with its offset, the parameter type and the connection's time zone. Do not retain only formatted hours and minutes, and do not export sensitive business fields alongside the diagnostic data.

Connection reuse and cleanup checks

This experiment uses SET LOCAL, so the settings apply only within the current transaction. A regular SET may remain in effect when the connection returns to the pool and affect the next borrower. The application should explicitly define the time zone used during connection initialization, then test settings after returning and borrowing connections again through the actual pool. Testing a single standalone connection is not enough.

To finish, run:

ROLLBACK;
SHOW TimeZone;

The final command should show the setting that was in effect before the transaction began. For the validation record, retaining the SQL, database version, session time zone and expected time differences is sufficient. If historical data really was written incorrectly, develop a separate migration plan: first identify the affected write paths, use known events to establish the original interpretation, then validate the correction on a copy. Only after those steps can you determine whether the data needs changing or the display alone needs fixing.

Frequently asked questions

Does timestamptz retain the original time zone name?

No. It stores an instant and renders it in the current session time zone. Store and validate the original zone name in a separate column when required.

Should historical timestamps be shifted to fix an hours-long discrepancy?

Not before checking column types, input offsets and the writing session's time zone against a known event. Fix display or session settings if the stored instant is already correct.

AMD EPYC 7543P Dedicated Server

From $444.5/month

Dedicated server configurations sized for your workload

The AMD EPYC 7543P is available with 128 GB or 256 GB of memory. Check the configuration page for available options.