Convert a bare timestamp to local time in PostgreSQL

Very often in old databases a timestamp column does not have the timezone info. This becomes an issue when the business gets bigger and crosses different timezones.

select bare_timestamp_field
  at time zone 'Asia/Shanghai'
  at time zone 'US/Pacific'
from some_table;

The above sample query assigns the Shanghai timezone to the bare timestamp, then convert the resulting timestamp w/ timezone to the US Pacific PST timestamp.

The double ‘at time zone’ parts can be read as: from timezone #1 to timezone #2. This is due to the implicit syntax. A more explicit syntax is the following, which makes more sense similar to type casting.

select ((bare_timestamp_field
         at time zone 'Asia/Shanghai')
        at time zone 'US/Pacific')
from some_table;

https://popsql.io/learn-sql/postgresql/how-to-convert-utc-to-local-time-zone-in-postgresql/

https://stackoverflow.com/questions/36341060/convert-a-utc-timezone-in-postgresql-to-est-local-time

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s