为何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
相关产品推荐
相关产品推荐

