【发布时间】: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