首页数据库PostgreSQL timestamp踩坑记录与填坑指南

PostgreSQL timestamp踩坑记录与填坑指南

时间2024-02-29 14:02:04发布访客分类数据库浏览624
导读:收集整理的这篇文章主要介绍了PostgreSQL timestamp踩坑记录与填坑指南,觉得挺不错的,现在分享给大家,也给大家做个参考。 项目Timezone情况NodeJS:UTC+0...
收集整理的这篇文章主要介绍了PostgreSQL timestamp踩坑记录与填坑指南,觉得挺不错的,现在分享给大家,也给大家做个参考。

项目Timezone情况

NodeJS:UTC+08

PostgreSQL:UTC+00

timestamptest.jsconst {
 Client }
     = require('pg')const client = new Client() client.connect()let sql = ``client.query(sql, (err, res) =>
 {
 console.LOG(err ? err.stack : res.rows[0].datetime) client.end()}
    )

不同时区to_timestamp查询结果

测试输入数据为1514736000(UTC时间2017-12-31 16:00:00,北京时间2018-01-01 00:00:00)

1、timezone=UTC

BEgin;
    SET TIME ZONE 'UTC';
    SELECT to_timestamp(1514736000) as datetime;
    END;
    

直接查询:2017-12-31 16:00:00+00YES

pg查询:2017-12-31T16:00:00.000ZYES

2、timezone=PRC

BEGIN;
    SET TIME ZONE 'PRC';
    SELECT to_timestamp(1514736000) as datetime;
    END;
    

直接查询:2018-01-01 00:00:00+08NO

pg查询:2017-12-31T16:00:00.000ZYES

PostgreSQL官方文档对timestamp的一个描述

详见:8.5.1.3. Time Stamps

In a lITeral that has been determined to be timestamp without time zone, PostgreSQL will silently ignore any time zone indication. That is, the resulting value is derived From the date/time fields in the input value, and is not adjusted for time zone.

使用to_timestamp进行时间转换且DB时区非UTC时,写入**timestamp without time zone**类型的COLUMN则会与预期结果不符。

不同Timezone/columnTyPE查询结果

1、timezone=UTC,timestamp with timezone

BEGIN;
    SET TIME ZONE 'UTC';
    SELECT TIMESTAMP WITH TIME ZONE '2017-12-31T16:00:00+00' as datetime;
    END;
    

直接查询:2017-12-31 16:00:00+00YES

pg查询:2017-12-31T16:00:00.000ZYES

2、timezone=UTC,timestamp without timezone

BEGIN;
    SET TIME ZONE 'UTC';
    SELECT TIMESTAMP '2017-12-31T16:00:00+00' as datetime;
    END;
    

直接查询:2017-12-31 16:00:00YES

pg查询:2017-12-31T08:00:00.000ZNO

3、timezone=PRC,timestamp with timezone

BEGIN;
    SET TIME ZONE 'PRC';
    SELECT TIMESTAMP WITH TIME ZONE '2017-12-31T16:00:00+00' as datetime;
    END;
    

直接查询:2018-01-01 00:00:00+08YES

pg查询:2017-12-31T16:00:00.000ZYES

4、timezone=PRC,timestamp without timezone

BEGIN;
    SET TIME ZONE 'PRC';
    SELECT TIMESTAMP '2017-12-31T16:00:00+00' as datetime;
    END;
    

直接查询:2017-12-31 16:00:00YES

pg查询:2017-12-31T08:00:00.000ZNO

据以上结果可判定:

使用pg查询**timestamp without time zone**类型的COLUMN时,会将数据库存储的时间当做北京时间而非UTC时间,与数据库时区没有关系。

总结

网上类似问题的解决办法是将DB时区改为UTC+08。

原理:写入DB的时间实际为北京时间,pg库恰好是当做北京时间读取,所以时间戳就不会出问题了。

假如应用部署在不同的地域,使用timestamp without time zone存储timestamp这样的设计简直是灾难。

不要用timestamp without time zone存储timestamp!

不要用timestamp without time zone存储timestamp!

不要用timestamp without time zone存储timestamp!

补充:pg查询时间间隔(timestamp类型)

create_date timestamp(6) without time zone

1.从2015-10-12到2015-10-13 之间的4点到9点的数据

select * from schedule where create_date between to_date('2015-10-12','yyyy-MM-dd') and to_date('2015-10-13','yyyy-MM-dd')and EXTRACT(hour from create_date) between 4 and 9;
    

结果:

2.2015-10-12五点的数据

select * from schedule where hospital_id='syzyyadmin' and date_trunc('hour',create_date)=to_timestamp('2015-10-12 05','YYYY-MM-DD HH24')

结果:

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

您可能感兴趣的文章:
  • PostgreSQL的generate_series()函数的用法说明
  • Postgresql通过查询进行更新的操作
  • 如何为PostgreSQL的表自动添加分区
  • postgresql 实现得到时间对应周的周一案例
  • PostgreSQL的upsert实例操作(insert on conflict do)
  • PostgreSQL 字符串拆分与合并案例
  • 浅谈PostgreSQL消耗的内存计算方法

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: PostgreSQL timestamp踩坑记录与填坑指南
本文地址: https://pptw.com/jishu/632959.html
hashmap和hashtable的数据结构是什么 mysql如何修改name数据类型

游客 回复需填写必要信息