如何为Postgres psql设置时区? [英] How to set timezone for Postgres psql?

查看:110
本文介绍了如何为Postgres psql设置时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将psql的时区设置为默认值(美国/中央)以外的其他值?到目前为止,这是我尝试过的操作:

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

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

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

推荐答案

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.

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

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

PGTZ=PST8PDT psql -c 'show timezone'

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

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 .bashrc file (if in Linux)

这篇关于如何为Postgres psql设置时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆