Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 494
gyorgyabraham Avatar asked Mar 19 '12 15:03

gyorgyabraham


People also ask

How do you do a timestamp without the time zone?

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.

Is of type timestamp with time zone?

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.

How does timestamp with timezone work?

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.


1 Answers

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.

like image 102
dbenhur Avatar answered Sep 21 '22 13:09

dbenhur