如何将 postgres 中的时区永久更改为 UTC?

Bha*_*wad 5 database postgresql datetime

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

- Locale and Formatting -

datestyle = 'iso, mdy'
#intervalstyle = 'postgres'
timezone = 'UTC'
#timezone_abbreviations = 'Default'
Run Code Online (Sandbox Code Playgroud)

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

Vao*_*sun 6

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

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

select pg_reload_conf();
Run Code Online (Sandbox Code Playgroud)

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

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 
Run Code Online (Sandbox Code Playgroud)

pending_restart false意味着你不需要重新启动postgres,但更改后仍然需要重新加载配置。正如您所看到的,此设置仅影响客户端。显然,客户端可以轻松地覆盖默认值。

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

为了永久地为某些用户设置此设置,请使用alter user uname set timezone和相同的方法来设置数据库的默认值。

  • 这是客户端配置 - 它没有永久设置。无论您在服务器端设置什么,如果您的客户端明智地设置其本地时区会话,它将覆盖您的“永久”设置 (3认同)

Erw*_*ter 1

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

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

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