The question is short: if I already have data in a column type timestamp without time zone, if I set the type to timestamp with time zone, what does postgresql do with this data?
There is no time zone information being stored in a timestamptz , but instead it is converted to UTC for storage. I'd say, always use timestamptz when the timestamps in question denote absolute time. That's all what timestamptz means.
It means that when you change the timezone of your database server, the timestamp value stored in the database will not change automatically. The timestamptz datatype is the timestamp with the time zone. The timestamptz datatype is a time zone-aware date and time data type.
For timestamp with time zone , the internally stored value is always in UTC (Universal Coordinated Time, traditionally known as Greenwich Mean Time, GMT ). An input value that has an explicit time zone specified is converted to UTC using the appropriate offset for that time zone.
It keeps the current value in localtime and sets the timezone to your localtime's offset:
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
According to the manual for Alter Table:
if [the USING clause is] omitted, the default conversion is the same as an assignment cast from old data type to new.
According to the manual for Date/Time types
Conversions between timestamp without time zone and timestamp with time zone normally assume that the timestamp without time zone value should be taken or given as timezone local time. A different time zone can be specified for the conversion using
AT TIME ZONE
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With