Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 在时区GMT插入当前时间戳将改为本地时间_Postgresql_Sql Timestamp - Fatal编程技术网

Postgresql 在时区GMT插入当前时间戳将改为本地时间

Postgresql 在时区GMT插入当前时间戳将改为本地时间,postgresql,sql-timestamp,Postgresql,Sql Timestamp,我有一个PostgreSQL数据库,其中发票表中有一个日期字段: Column | Type | payment_date | timestamp with time zone | 服务器位于GMT-5,如您所见: $date Tue Jan 22 17:33:01 EST 2019 通过PostgreSQL命令获取GMT时间可以提供: select current_timestamp at time zone 'GMT'

我有一个PostgreSQL数据库,其中发票表中有一个日期字段:

Column           |           Type           |
payment_date     | timestamp with time zone | 
服务器位于GMT-5,如您所见:

$date
Tue Jan 22 17:33:01 EST 2019
通过PostgreSQL命令获取GMT时间可以提供:

select current_timestamp at time zone 'GMT';
          timezone
----------------------------
 2019-01-22 22:33:01.087354
2019-01-22 22:33:01.087354-05 
问题是当我执行插入/更新时:

update invoices set payment_date = current_timestamp at time zone 'GMT'
然后,当我得到查询结果时

select payment_date from invoices
它给了我:

select current_timestamp at time zone 'GMT';
          timezone
----------------------------
 2019-01-22 22:33:01.087354
2019-01-22 22:33:01.087354-05 
错了应该给我2019-01-22 22:33:01.087354-00


我做错了什么?

您的字段<代码>付款日期是(正确的)类型<代码>带时区的时间戳,因此使用会话时区转换
无时区的时间戳
操作的结果

PostgreSQL的带有时区的
时间戳
数据类型不存储时区信息,它存储转换为显示会话时区的UTC时间戳


建议您将时间戳存储为带时区的时间戳,并在显示时将其转换为所需的时区。

您的字段
付款日期
是(正确的)类型
带时区的时间戳
,因此,不带时区的
时间戳
在时区
操作的结果,将使用会话时区进行转换

PostgreSQL的带有时区的
时间戳
数据类型不存储时区信息,它存储转换为显示会话时区的UTC时间戳

最好将时间戳存储为带时区的时间戳,并在显示时将其转换为所需的时区