问题很简单:如果我已经在没有时区的列类型timestamp中有数据,如果我将类型设置为带有时区的timestamp,postgresql会如何处理这些数据?
问题很简单:如果我已经在没有时区的列类型timestamp中有数据,如果我将类型设置为带有时区的timestamp,postgresql会如何处理这些数据?
它将当前值保留为localtime,并将时区设置为localtime的偏移量:
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年的手册:
如果省略[the USING clause is],默认转换与从旧数据类型转换为新数据类型的赋值转换相同.
根据Date/Time types年的手册
timestamp without time zone和timestamp with time zone之间的转换通常假定timestamp without time zone值应作为或给定为timezone本地时间.可以使用
AT TIME ZONE
为转换指定不同的时区.