How do I get the current timezone name in Postgres 9.3?

158,709

Solution 1

I don't think this is possible using PostgreSQL alone in the most general case. When you install PostgreSQL, you pick a time zone. I'm pretty sure the default is to use the operating system's timezone. That will usually be reflected in postgresql.conf as the value of the parameter "timezone". But the value ends up as "localtime". You can see this setting with the SQL statement.

show timezone;

But if you change the timezone in postgresql.conf to something like "Europe/Berlin", then show timezone; will return that value instead of "localtime".

So I think your solution will involve setting "timezone" in postgresql.conf to an explicit value rather than the default "localtime".

Solution 2

It seems to work fine in Postgresql 9.5:

SELECT current_setting('TIMEZONE');

Solution 3

This may or may not help you address your problem, OP, but to get the timezone of the current server relative to UTC (UT1, technically), do:

SELECT EXTRACT(TIMEZONE FROM now())/3600.0;

The above works by extracting the UT1-relative offset in minutes, and then converting it to hours using the factor of 3600 secs/hour.

Example:

SET SESSION timezone TO 'Asia/Kabul';
SELECT EXTRACT(TIMEZONE FROM now())/3600.0;
-- output: 4.5 (as of the writing of this post)

(docs).

Solution 4

You can access the timezone by the following script:

SELECT * FROM pg_timezone_names WHERE name = current_setting('TIMEZONE');
  • current_setting('TIMEZONE') will give you Continent / Capital information of settings
  • pg_timezone_names The view pg_timezone_names provides a list of time zone names that are recognized by SET TIMEZONE, along with their associated abbreviations, UTC offsets, and daylight-savings status.
  • name column in a view (pg_timezone_names) is time zone name.

output will be :

name- Europe/Berlin, 
abbrev - CET, 
utc_offset- 01:00:00, 
is_dst- false

Solution 5

See this answer: Source

If timezone is not specified in postgresql.conf or as a server command-line option, the server attempts to use the value of the TZ environment variable as the default time zone. If TZ is not defined or is not any of the time zone names known to PostgreSQL, the server attempts to determine the operating system's default time zone by checking the behavior of the C library function localtime(). The default time zone is selected as the closest match among PostgreSQL's known time zones. (These rules are also used to choose the default value of log_timezone, if not specified.) source

This means that if you do not define a timezone, the server attempts to determine the operating system's default time zone by checking the behavior of the C library function localtime().

If timezone is not specified in postgresql.conf or as a server command-line option, the server attempts to use the value of the TZ environment variable as the default time zone.

It seems to have the System's timezone to be set is possible indeed.

Get the OS local time zone from the shell. In psql:

=> \! date +%Z
Share:
158,709

Related videos on Youtube

Deutro
Author by

Deutro

Test

Updated on March 19, 2020

Comments

  • Deutro
    Deutro over 4 years

    I want to get the current timezone name. What I already achieved is to get the utc_offset / the timezone abbreviation via:

    SELECT * FROM pg_timezone_names WHERE abbrev = current_setting('TIMEZONE')
    

    This gives me all Continent / Capital combinations for this timezone but not the exact timezone. For example I get:

    Europe/Amsterdam
    Europe/Berlin
    

    The server is in Berlin and I want to get the timezone name of the server.

    The problem I have with CET that it is always UTC+01:00 and does not account for DST iirc.

    • Patrick
      Patrick over 9 years
      The proper time zone name for either of Berlin and Amsterdam is Central European Time (CET). In general, time zone names and abbreviations are not well defined; while there is an ISO standard many countries use their own definitions. PostgreSQL support is also not complete, see postgresql.org/docs/9.4/static/datetime-config-files.html for some details.
    • Deutro
      Deutro over 9 years
      In the pg_timezone_names table CET is defined as abbreviation and e.g "Europe/Berlin" as name. I need the name and not the abbreviation.
    • Patrick
      Patrick over 9 years
      The link I gave in my previous comment shows you how you can edit the files to provide what you need. That is as good as it gets.
    • Deutro
      Deutro over 9 years
      So there is no way for postgres to tell me wether I am in "Europe/Berlin" or "Europe/Amsterdam" just that I am in the timezone CET?
    • Patrick
      Patrick over 9 years
      Can you edit your question and define "I am in"? A server is (usually) in a fixed location and the time zone is taken from the operating system or the configuration file. The tz name is as fixed as the server. So do you indeed want the time zone name of the server or of data in the database?
    • Deutro
      Deutro over 9 years
      Edited my question. Hope this helps you to understand what I really want to achieve.
    • Hartley Brody
      Hartley Brody over 5 years
      This isn't totally relevant to the original question, but a related command that's good to to know -- changing the database's timezone is simply set timezone to 'UTC';
  • Deutro
    Deutro over 9 years
    Thanks for your answer. Kind of strange that it is not possible to get the Systems timezone.
  • koyae
    koyae about 8 years
    Another user suggested using SELECT EXTRACT(TIMEZONE_HOUR FROM now()), but this is mildly dangerous because it ignores the fact that there are both half and quarter timezones out there. @giladMayani
  • koyae
    koyae about 8 years
    "North Korea, Newfoundland, India, Iran, Afghanistan, Venezuela, Burma, Sri Lanka, the Marquesas, as well as parts of Australia use half-hour deviations from standard time. Some nations, such as Nepal, and some provinces, such as the Chatham Islands, use quarter-hour deviations." (link)
  • ivkremer
    ivkremer almost 7 years
    And to set timezone you can use set timezone to 'UTC'.
  • Mike Sherrill 'Cat Recall'
    Mike Sherrill 'Cat Recall' almost 7 years
    @ivkremer: set timezone sets the timezone for one client session; it doesn't set the timezone for the PostgreSQL server.
  • ivkremer
    ivkremer almost 7 years
    @MikeSherrill'CatRecall' thank you, I didn't check it.
  • Eugen Konkov
    Eugen Konkov almost 6 years
    date +%Z command will return client's timezone, not server's you have connected via psql
  • Subhendu Mahanta
    Subhendu Mahanta almost 4 years
    I ran your command & it gave me back 15,200 rows, though all of them are same timezone.
  • koyae
    koyae about 3 years
    @SubhenduMahanta sounds like you (or your IDE) included a FROM clause. You don't need one in this particular case.