postgresql postgres 默认时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6663765/
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:00:47  来源:igfitidea点击:

postgres default timezone

postgresql

提问by Blu Towers

I installed PostgreSQL 9and the time it is showing is 1 hour behind the server time.

我安装了PostgreSQL 9,它显示的时间比服务器时间晚 1 小时。

Running Select NOW()shows: 2011-07-12 11:51:50.453842+00

跑步Select NOW()节目:2011-07-12 11:51:50.453842+00

The server date shows: Tue Jul 12 12:51:40 BST 2011

服务器日期显示: Tue Jul 12 12:51:40 BST 2011

It is 1 hour behind but the timezone shown in phppgadminis: TimeZone Etc/GMT0

晚了 1 小时,但显示的时区phppgadmin是:TimeZone Etc/GMT0

I have tried going into the postgresql.confand setting

我试过进入postgresql.conf和设置

timezone = GMT

时区 = GMT

then running a restart but no change.

然后运行重新启动但没有变化。

Any ideas I thought it would have just used the server timezone but obviously not?!

我认为它只会使用服务器时区的任何想法,但显然不是?!

SOLUTION!: I did set to GMTbefore and it was an hour behind. after searching around turns out that I needed to set it to Europe/London. This takes into account the +1 hour in British summer time, GMT does not!

解决方案!:我GMT之前确实设置过,但晚了一个小时。四处搜索后发现我需要将其设置为Europe/London. 这考虑到了英国夏令时的 +1 小时,GMT 没有!

回答by Muhammad Usama

The time zone is a session parameter. So, you can change the timezone for the current session.

时区是一个会话参数。因此,您可以更改当前会话的时区。

See the doc.

请参阅文档

set timezone TO 'GMT';


Or, more closely following the SQL standard, use the SET TIME ZONEcommand. Notice two words for "TIME ZONE" where the code above uses a single word "timezone".

或者,更严格地遵循 SQL 标准,使用该SET TIME ZONE命令。请注意“TIME ZONE”的两个词,其中上面的代码使用了一个词“timezone”。

SET TIME ZONE 'UTC';

The doc explains the difference:

该文档解释了差异:

SET TIME ZONE extends syntax defined in the SQL standard. The standard allows only numeric time zone offsets while PostgreSQL allows more flexible time-zone specifications. All other SET features are PostgreSQL extensions.

SET TIME ZONE 扩展了 SQL 标准中定义的语法。该标准只允许数字时区偏移,而 PostgreSQL 允许更灵活的时区规范。所有其他 SET 功能都是 PostgreSQL 扩展。

回答by Iurii Perevertailo

Choose a timezonefrom:

timezone从以下选项中选择一个:

SELECT * FROM pg_timezone_names;

And setas below given example:

set下面给出的例子:

ALTER DATABASE postgres SET timezone TO 'Europe/Berlin';

Use your DB name in place of postgresin above statement.

使用您的数据库名称代替postgres上述语句。

回答by guerrerocarlos

To acomplish the timezone change in Postgres 9.1 you must:

要在 Postgres 9.1 中完成时区更改,您必须:

1.-Search in your "timezones" folder in /usr/share/postgresql/9.1/ for the appropiate file, in my case would be "America.txt", in it, search for the closest location to your zone and copy the first letters in the left column.

1.-在 /usr/share/postgresql/9.1/ 的“timezones”文件夹中搜索合适的文件,在我的情况下是“America.txt”,在其中搜索离您的区域最近的位置并复制左栏中的第一个字母。

For example: if you are in "New York" or "Panama" it would be "EST":

例如:如果您在“纽约”或“巴拿马”,它将是“EST”:

#  - EST: Eastern Standard Time (Australia)
EST    -18000    # Eastern Standard Time (America)
                 #     (America/New_York)
                 #     (America/Panama)

2.-Uncomment the "timezone" line in your postgresql.conffile and put your timezone as shown:

2.-取消注释文件中的“时区”行postgresql.conf并将时区设置为如下所示:

#intervalstyle = 'postgres'
#timezone = '(defaults to server environment setting)'
timezone = 'EST'
#timezone_abbreviations = 'EST'     # Select the set of available time zone
                                        # abbreviations.  Currently, there are
                                        #   Default
                                        #   Australia

3.-Restart Postgres

3.-重启 Postgres

回答by Basil Bourque

The accepted answerby Muhammad Usamais correct.

接受的答案穆罕默德·乌萨马·本是正确的。

Configuration Parameter Name

配置参数名称

That answer shows how to set a Postgres-specific configuration parameter with the following:

该答案显示了如何使用以下内容设置 Postgres 特定的配置参数:

SET timezone TO 'UTC';

…where timezoneis not a SQL command, it is the name of the configuration parameter.

... wheretimezone不是 SQL 命令,它是配置参数的名称。

See the doc for this.

请参阅此文档

Standard SQL Command

标准 SQL 命令

Alternatively, you can use the SQL command defined by the SQL spec: SET TIME ZONE. In this syntax a pair of words TIME ZONEreplace "timezone" (actual SQL command versus parameter name), and there is no "TO".

或者,您可以使用 SQL 规范定义的 SQL 命令:SET TIME ZONE。在此语法中,一对词TIME ZONE替换了“时区”(实际 SQL 命令与参数名称),并且没有“TO”。

SET TIME ZONE 'UTC';

Both this command and the one above have the same effect, to set the value for the current session only. To make the change permanent, see this sibling answer.

此命令和上面的命令具有相同的效果,仅设置当前会话的值。要使更改永久化,请参阅此兄弟答案

See the doc for this.

请参阅此文档

Time Zone Name

时区名称

You can specify a proper time zone name. Most of these are continent/region.

您可以指定正确的时区名称。其中大部分是大陆/地区。

SET TIME ZONE 'Africa/Casablanca';

…or…

…或者…

SET TIME ZONE 'America/Montreal';

Avoid the 3 or 4 letterabbreviations such as ESTor ISTas they are neither standardized nor unique.

避免使用 3 或 4 个字母的缩写,例如EST或 ,IST因为它们既不标准化也不独特。

Get current zone

获取当前区域

To see the current time zone for a session, try either of the following statements. Technically we are calling the SHOWcommand to display a run-time parameter.

要查看会话的当前时区,请尝试以下任一语句。从技术上讲,我们调用SHOW命令来显示运行时参数。

SHOW timezone ;

…or…

…或者…

SHOW time zone ;

US/Pacific

美国/太平洋

回答by Pascal

In addition to the previous answers, if you use the tool pgAdmin IIIyou can set the time zone as follows:

除了前面的答案,如果您使用工具pgAdmin III,您可以按如下方式设置时区:

  1. Open Postgres config via Tools > Server Configuration > postgresql.conf
  2. Look for the entry timezoneand double click to open
  3. Change the Value
  4. Reload Server to apply configuration changes (Play button on top or via services)
  1. 通过工具 > 服务器配置 > postgresql.conf打开 Postgres 配置
  2. 寻找入口时区,双击打开
  3. 更改值
  4. 重新加载服务器以应用配置更改(顶部的播放按钮或通过服务)

Postgres config in pgAdmin III

pgAdmin III 中的 Postgres 配置

回答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

“数据库源属性”->“高级”选项卡->“VM 选项”:-Duser.timezone=UTC+06:00

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

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

回答by MGrillo

Maybe not related to the question, but I needed to use CST, set the system timezone to the desired tz (America/...) and then in postgresql conf set the value of the timezone to 'localtime' and it worked as a charm, current_time printing the right time (Postgresql 9.5 on Ubuntu 16)

也许与问题无关,但我需要使用 CST,将系统时区设置为所需的 tz(美国/...),然后在 postgresql conf 中将时区的值设置为“localtime”,它起到了很好的作用, current_time 打印正确的时间(Ubuntu 16 上的 Postgresql 9.5)