我想在几个表中的UTC时间戳“创建”字段。Postgres UTC时间戳?
ts TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
给我的本地时间与时区:
foo=# select ts from messages;
ts
-------------------------------
2017-05-26 11:54:07.532796+01
2017-05-26 11:54:08.536241+01
2017-05-26 11:54:09.538119+01
2017-05-26 11:54:10.541089+01
2017-05-26 11:54:11.543262+01
ts TIMESTAMP NOT NULL DEFAULT now(),
给我本地时间没有时区信息(危险):
foo=# select ts from messages;
ts
----------------------------
2017-05-26 11:56:00.134596
2017-05-26 11:56:01.13798
2017-05-26 11:56:02.140586
2017-05-26 11:56:03.143076
2017-05-26 11:56:04.14565
什么字段定义会给我下面,当代码在非UTC时区(例如BST,英国夏令时)的服务器上运行时:
foo=# select ts from messages;
ts
----------------------------
2017-05-26 10:56:00.134596+00
2017-05-26 10:56:01.13798+00
2017-05-26 10:56:02.140586+00
2017-05-26 10:56:03.143076+00
2017-05-26 10:56:04.14565+00
连:
foo=# select ts from messages;
ts
----------------------------
2017-05-26 10:56:00.134596
2017-05-26 10:56:01.13798
2017-05-26 10:56:02.140586
2017-05-26 10:56:03.143076
2017-05-26 10:56:04.14565
就可以了,如果不出意外是可能的(注:时间为上午10点UTC,不是上午11点BST在这两个例子以上)。
timestamptz会做。您的客户端时区将运行时间,该时间值将转换为UTC并保存到服务器。 postgres以UTC格式保存所有时间戳 –