【问题标题】:How do I change the timezone to UTC in postgres permanently?如何在 postgres 中将时区永久更改为 UTC?
【发布时间】:2017-12-09 10:35:59
【问题描述】:

我在 postgresql.conf 中设置时区如下:-

- Locale and Formatting -

datestyle = 'iso, mdy'
#intervalstyle = 'postgres'
timezone = 'UTC'
#timezone_abbreviations = 'Default'

但是当我在 postgres 服务器中看到时区时,它默认为“亚洲/加尔各答”。 我已经在 MacOs 中使用 brew 包安装了 postgres。

【问题讨论】:

    标签: database postgresql datetime


    【解决方案1】:

    postgresql.conf 中设置timezone 只是为没有此设置的客户端设置默认值。如果您看到其他值,则意味着客户端将其设置为某个本地值。

    或者你没有重新加载服务器:

    select pg_reload_conf();
    

    要检查设置以及启用它所需的条件,您可以:

    t=# select * from pg_settings where name = 'TimeZone';
    -[ RECORD 1 ]---+----------------------------------------------------------------
    name            | TimeZone
    setting         | UTC
    unit            |
    category        | Client Connection Defaults / Locale and Formatting
    short_desc      | Sets the time zone for displaying and interpreting time stamps.
    extra_desc      |
    context         | user
    vartype         | string
    source          | configuration file
    min_val         |
    max_val         |
    enumvals        |
    boot_val        | GMT
    reset_val       | UTC
    sourcefile      | /Users/vao/t96/postgresql.conf
    sourceline      | 556
    pending_restart | f 
    

    pending_restartfalse表示不需要重启postgres,但是修改后还是需要重新加载config。此外,正如您所见,此设置仅影响客户端。显然,客户端可以轻松覆盖默认值。

    要设置时区客户端,只需运行set timezone to 'UTC'

    要为某些用户永久设置此设置,请使用alter user uname set timezone 和为 db 设置默认值相同。

    【讨论】:

    • 那么现在如何将时区永久设置为“UTC”,因为它仍将时区值显示为“亚洲/加尔各答”?
    • 这是客户端配置 - 它没有永久设置。无论您在服务器端设置什么,如果您的客户端明智地设置其本地时区会话,它将覆盖您的“永久”设置
    【解决方案2】:

    有很多方法可以设置配置变量。但是大多数这些设置都可以在其事务中被客户端否决。 (有些设置需要重启服务器或有其他限制,但timezone没有。)

    为了绝对确定,timezone = 'UTC' 会发生一些操作,您可以将其封装在具有自己设置的服务器端函数中,覆盖任何用户设置。在此相关答案中查找代码示例:

    虽然这符合您的问题的措辞,但我认为这不是您要寻找的。您可能只需要在更改 postgresql.conf重新加载以设置新会话的默认值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-07
      • 2014-01-09
      • 1970-01-01
      • 2020-08-31
      相关资源
      最近更新 更多