如何为Postgres psql设置时区?

dfr*_*kow 25 postgresql timezone psql

如何将psql的时区设置为默认值(US/Central)以外的其他值?这是我到目前为止所尝试的:

$ psql
psql (9.1.4, server 9.0.4)
...

$ psql -c 'show timezone'
  TimeZone  
------------
 US/Central

$ psql --set=timezone=US/Eastern -c 'show timezone'
  TimeZone  
------------
 US/Central

$ psql --variable=timezone=US/Eastern -c 'show timezone'
  TimeZone  
------------
 US/Central
Run Code Online (Sandbox Code Playgroud)

编辑:我不想更改服务器时区,只是客户端.

编辑#2:我想要它以非交互模式.

Ric*_*ton 25

psql (9.1.4)
Type "help" for help.

richardh=> show timezone;
 TimeZone 
----------
 GB
(1 row)

richardh=> set timezone='UTC';
SET
richardh=> show timezone;
 TimeZone 
----------
 UTC
(1 row)

richardh=> set timezone='US/Eastern';
SET
richardh=> show timezone;
  TimeZone  
------------
 US/Eastern
(1 row)

richardh=> set timezone='blah';
ERROR:  invalid value for parameter "TimeZone": "blah"
Run Code Online (Sandbox Code Playgroud)


leo*_*loy 18

psql的医生说:

-v assignment
--set=assignment
--variable=assignment
Perform a variable assignment, like the \set internal command. Note that 
you must separate name and value, if any, by an equal sign on the command line....
Run Code Online (Sandbox Code Playgroud)

但是对于时区它似乎不起作用,也许是因为这样:

 These assignments are done during a very early stage of start-up, 
 so variables reserved for internal purposes might get overwritten later.
Run Code Online (Sandbox Code Playgroud)

因此,似乎您必须在psql中使用SET命令,或者设置PGTZ环境变量:

PGTZ=PST8PDT psql -c 'show timezone'
Run Code Online (Sandbox Code Playgroud)

当然,如果您可以为用户设置全局时区(不仅仅是针对此单独的psql实例),您可以在其.bashrc文件中设置该变量(如果在Linux中)

  • 请注意,您不需要导出它。在bash提示符下运行:PGTZ = US / Eastern psql -c'show timezone' (2认同)

Abh*_*hra 18

ALTER USER postgres SET timezone ='Asia/Tokyo';

  • 要更改数据库的时区:“ALTER DATABASE database_name SET TIMEZONE='zone';”,然后“SET TIMEZONE='zone';”。 (3认同)