You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何pg_typeof(TIMESTAMP '2004-10-19 10:23:54+02')返回timestamp without time zone?

问题原因解析

这是因为PostgreSQL的类型关键字规则:

  • TIMESTAMP 是 timestamp without time zone 的简写
  • TIMESTAMPTZ 或 TIMESTAMP WITH TIME ZONE 才是 timestamp with time zone 的简写

当你执行 select pg_typeof(TIMESTAMP '2004-10-19 10:23:54+02'); 时,即便输入字符串带时区偏移,TIMESTAMP 关键字会强制将其转换为不带时区的timestamp类型——PostgreSQL会先根据时区偏移把时间转成当前会话时区的时间,然后丢弃时区信息,最终存储为timestamp without time zone。

如果要得到timestamp with time zone类型,需要把关键字改成TIMESTAMPTZ或者TIMESTAMP WITH TIME ZONE,比如执行:

select pg_typeof(TIMESTAMPTZ '2004-10-19 10:23:54+02');

或者

select pg_typeof(TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02');

此时返回结果就是timestamp with time zone了。

内容的提问来源于stack exchange,提问作者Han Qi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 11:48:10