简体   繁体   中英

Convert To Another Timezone in Postgresql

How do I convert the below code to another timezone using postgresql?

SELECT date_trunc('month', CURRENT_DATE);

The above code returns this output: '2020-05-01 00:00:00'

I want to convert from the default timezone to 'PST'.

Is this what you are looking for?

select date_trunc('month', current_date) at time zone 'utc' at time zone 'pst'

Yields:

2020-05-01 08:00:00+01

The following is not specific to this question.
While Postgres supports timezone abbreviations (pst, ...) be cautious using them. They establish fixed offsets from UTC. That offset remains the same all time; they are not aware of day light saving changes. In most cases much better to use full timezone name, ie 'America/Los_Angeles' instead. From Postgres Documentation

A time zone abbreviation, for example PST. Such a specification merely defines a particular offset from UTC, in contrast to full time zone names which can imply a set of daylight savings transition-date rules as well. The recognized abbreviations are listed in the pg_timezone_abbrevs view (see Section 51.91). You cannot set the configuration parameters TimeZone or log_timezone to a time zone abbreviation, but you can use abbreviations in date/time input values and with the AT TIME ZONE operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM