postgresql 如何为 Postgres psql 设置时区?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11779293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:38:01  来源:igfitidea点击:

How to set timezone for Postgres psql?

postgresqltimezonepsql

提问by dfrankow

How do I set timezone for psql to something other than my default (US/Central)? Here's what I've tried so far:

如何将 psql 的时区设置为默认值(美国/中部)以外的时区?这是我迄今为止尝试过的:

$ 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

Edit: I don't want to change the server timezone, just the client.

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

Edit #2: I want it in non-interactive mode.

编辑 #2:我希望它处于非交互模式。

采纳答案by leonbloy

The psqldoc says:

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....

But with the timezone it does not seem to work, perhaps because because of this:

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

 These assignments are done during a very early stage of start-up, 
 so variables reserved for internal purposes might get overwritten later.

So, it seems you must either use the SET command inside psql, or either set the PGTZenvironment variable:

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

PGTZ=PST8PDT psql -c 'show timezone'

Of course, if you are OK with setting the timezone globally for the user (not just for this individual psql instance), you might set that variable in its .bashrcfile (if in Linux)

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

回答by Richard Huxton

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"

回答by Abhilash Mishra

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

回答by ARA1307

Note many third-party clients have own timezone settings overlapping any Postgres server and\or session settings.

请注意,许多第三方客户端都有自己的时区设置,与任何 Postgres 服务器和/或会话设置重叠。

E.g. if you're using 'IntelliJ IDEA 2017.3' (or DataGrips), you should define timezone as:

例如,如果您使用的是“IntelliJ IDEA 2017.3”(或 DataGrips),则应将时区定义为:

'DB source properties' -> 'Advanced' tab -> 'VM Options': -Duser.timezone=UTC+06:00

'DB source properties' -> 'Advanced' tab -> 'VM Options': -Duser.timezone=UTC+06:00

otherwise you will see 'UTC' despite of whatever you have set anywhere else.

否则,无论您在其他任何地方设置了什么,您都会看到“UTC”。