Re: Timezone handling with timestamp without time zone columns - Mailing list pgsql-general

From Nandish Bhuva
Subject Re: Timezone handling with timestamp without time zone columns
Date
Msg-id YT4P288MB0150006B32BC0AEE934E8ECA837FA@YT4P288MB0150.CANP288.PROD.OUTLOOK.COM
Whole thread Raw
In response to Timezone handling with timestamp without time zone columns  (Nandish Bhuva <Nandish.bhuva@srmsoftwareinc.com>)
Responses Re: Timezone handling with timestamp without time zone columns
Re: Timezone handling with timestamp without time zone columns
List pgsql-general
Adding pgsql-general@lists.postgresql.org +++

Regards,
Nandish Bhuva

From: Nandish Bhuva <Nandish.bhuva@srmsoftwareinc.com>
Sent: Tuesday, March 3, 2026 3:01 PM
To: Laurenz Albe <laurenz.albe@cybertec.at>
Subject: Re: Timezone handling with timestamp without time zone columns
 

Thank you for your response and for clarifying that the issue stems from how the timestamps are being stored rather than from PostgreSQL itself.
Unfortunately, the application is quite large and complex, and at this time we are not in a position to modify the column definitions or update the stored data. Therefore, we are looking for a solution that allows us to handle the timezone conversion purely at the query level using SELECT, without altering the table structure or existing data.
As mentioned previously:
Our goal is to convert both timestamps to a common timezone (for example, UTC) within the query itself to ensure accurate comparison.
I attempted the following:

    (to_timestamp('2026-02-19 01:23:46.016',
    'YYYY-MM-DD HH24:MI:SS.FF3')
    AT TIME ZONE 'Canada/Pacific')
    AT TIME ZONE 'UTC' AS utc_time;

However, the result does not appear to be converting correctly in our actual comparison scenario.
Could you please advise on the correct way to:
Your guidance on the proper AT TIME ZONE usage for timestamp without time zone columns would be greatly appreciated.
Thank you again for your assistance.
Regards,
Nandish Bhuva

From: Laurenz Albe <laurenz.albe@cybertec.at>
Sent: Friday, February 27, 2026 8:31 PM
To: Nandish Bhuva <Nandish.bhuva@srmsoftwareinc.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Timezone handling with timestamp without time zone columns
 
On Wed, 2026-02-25 at 08:28 +0000, Nandish Bhuva wrote:
> I would like to report a timezone-related issue we are encountering in our PostgreSQL database.

To avoid misunderstandings: thsi is not a problem of PostgreSQL, but a user-created
problem, right?

> We have two columns:
>  * empjob_utc_update_date
>  * jstsk_lst_end_tm
>
> Both columns are defined as timestamp without time zone.
> Currently, we are observing the following values:
>  * empjob_utc_update_date → 2026-02-19 06:26:23.830811
>  * jstsk_lst_end_tm → 2026-02-19 01:23:46.016
>
> Our entire application runs in the Canada/Pacific timezone. However, when comparing
> these two timestamps in our queries, we are getting incorrect results in the system.
> It appears that:
>  * empjob_utc_update_date is effectively storing UTC time.
>  * jstsk_lst_end_tm is storing Canada/Pacific local time.

To reiterate: *you* are storing the data in the columns in this way.

>  * Since both columns are defined as timestamp without time zone, PostgreSQL does not
>    apply any timezone conversion during comparison, which is leading to logical
>    inconsistencies.
>
> We would like clarification on the recommended approach to handle this scenario. Specifically:
>    1. Should both columns be converted to timestamp with time zone

If you are operating only within a single time zone, it doesn't matter.
You just have to be consistent about how you store timestamps.

>    2. Give me best solution for without even changing the column datatype.

You can fix the incorrectly stored data with

  UPDATE tab
  SET empjob_utc_update_date =
      empjob_utc_update_date AT TIME ZONE 'UTC'
                             AT TIME ZONE 'America/Chicago';

That will convert UTC timestamps to Chicago timestamps.

> Please advise on the best practice to ensure consistent timezone handling and accurate
> comparisons going forward.

The best practice is that you store tmestamps in a consistent fashion:

either

- use "timestamp with time zone", store timestamps with time zone
  and make sure that the parameter "timezone" is set correctly in each
  database session

or

- use "timestamp without time zone" and store only Chicago timestamps
  without a time zone

Yours,
Laurenz Albe
Attachment

pgsql-general by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: Documentation weirdness
Next
From: Laurenz Albe
Date:
Subject: Re: Timezone handling with timestamp without time zone columns