PostgreSQL alter type timestamp without time zone – > with time zone

前端之家收集整理的这篇文章主要介绍了PostgreSQL alter type timestamp without time zone – > with time zone前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
问题很简单:如果我已经在一个列类型时间戳没有时区的数据,如果我设置类型为时间戳与时区,postgresql做这个数据是什么?
它保持当前值在本地时间,并将时区设置为本地时间的偏移量:
create table a(t timestamp without time zone,t2 timestamp with time zone);
insert into a(t) values ('2012-03-01'::timestamp);
update a set t2 = t;
select * from a;
          t          |           t2           
---------------------+------------------------
 2012-03-01 00:00:00 | 2012-03-01 00:00:00-08

alter table a alter column t type timestamp with time zone;
select * from a;
           t            |           t2           
------------------------+------------------------
 2012-03-01 00:00:00-08 | 2012-03-01 00:00:00-08

根据Alter Table手册:

if @H_403_11@[the USING clause is] omitted,the default conversion is the same as an assignment cast from old data type to new.

根据Date/Time types手册

Conversions between @H_403_11@timestamp without time zone and @H_403_11@timestamp with time zone normally assume that the @H_403_11@timestamp without time zone value should be taken or given as @H_403_11@timezone local time. A different time zone can be specified for the conversion using AT TIME ZONE.

猜你在找的Postgre SQL相关文章