【问题标题】:Unable to set default value to timestamptz-type column in postgres while DB timezone setting is different than UTC当数据库时区设置与 UTC 不同时,无法在 postgres 中将默认值设置为 timestamptz 类型的列
【发布时间】:2022-01-20 02:31:56
【问题描述】:

我要创建的表

请注意ts 列的默认值。来自psql

root=# create temporary table test (
id int,
ts timestamptz not null default '0001-01-01 00:00:00 Z'
);

容器没有设置环境变量来更改时区

root=# show timezone;
 TimeZone 
----------
 UTC
(1 row)

创建表的结果(正确的默认值):

root=# \d+ test
                                                                       Table "pg_temp_3.test"
 Column |           Type           | Collation | Nullable |                      Default                       | Storage | Compression | Stats target | Description 
--------+--------------------------+-----------+----------+----------------------------------------------------+---------+-------------+--------------+-------------
 id     | integer                  |           |          |                                                    | plain   |             |              | 
 ts     | timestamp with time zone |           | not null | '0001-01-01 00:00:00+00'::timestamp with time zone | plain   |             |              | 
Access method: heap

注意:此默认值是预期的行为,但我需要将容器设置为不同的时区,以便 now() 函数正常工作。 now() 函数在另一列中用作默认值。

具有正确时区的容器

设置时区:

$ docker run --name postgres_test -p 5432:5432 -e "TZ=Brazil/East" -e POSTGRES_USER=root -e POSTGRES_PASSWORD=my_password -d postgres:14-alpine

来自psql正确的时区):

root=# show timezone;
  TimeZone   
-------------
 Brazil/East
(1 row)

选择当前时间(正确返回):

root=# select now();
              now              
-------------------------------
 2021-12-17 01:19:07.720302-03
(1 row)

但是,当我创建表时,我会为 ts 列返回 不正确的默认值

root=# \d+ test
                                                                           Table "pg_temp_3.test"
 Column |           Type           | Collation | Nullable |                           Default                           | Storage | Compression | Stats target | Description 
--------+--------------------------+-----------+----------+-------------------------------------------------------------+---------+-------------+--------------+-------------
 id     | integer                  |           |          |                                                             | plain   |             |              | 
 ts     | timestamp with time zone |           | not null | '0001-12-31 20:53:32-03:06:28 BC'::timestamp with time zone | plain   |             |              | 
Access method: heap

默认值设置为'0001-12-31 20:53:32-03:06:28 BC',但我需要的是'0001-01-01 00:00:00+00'

这个“奇怪”的值显然与我的时区设置有关。

我无法弄清楚为什么会发生这种情况,也无法解决如何解决。如果有人可以帮助我,我会很高兴。提前致谢。

注意:在 Go 中,值 '0001-01-01 00:00:00+00'time.Time 数据类型的零值。

【问题讨论】:

    标签: postgresql timezone


    【解决方案1】:

    结果完全正确。 0001-12-31 20:53:32-03:06:28 BC0001-01-01 00:00:00+00 相同。如果您不希望时间戳显示为当时巴西挂钟的最佳近似值,请将timezone 设置为不同的值。

    如果要忽略时区,可以使用数据类型timestamp without time zone

    【讨论】:

      猜你喜欢
      • 2020-07-08
      • 1970-01-01
      • 2021-12-24
      • 2012-10-21
      • 1970-01-01
      • 2018-06-20
      • 2011-09-03
      • 2011-08-14
      • 1970-01-01
      相关资源
      最近更新 更多