How can I change VARCHAR() type to DATETIME using ALTER in Postgresql? And my table column has already data like: "2013-12-08 16:09:07 "
You want the USING
clause to ALTER TABLE ... ALTER COLUMN ... TYPE
, and the to_timestamp
function .
ALTER TABLE mytable
ALTER COLUMN thecolumn
TYPE TIMESTAMP WITH TIME ZONE
USING to_timestamp(thecolumn, 'YYYY-MM-DD HH24:MI:SS');
In this case as the data looks like it's already a valid timestamp you can probably simplify it with a cast instead:
ALTER TABLE mytable
ALTER COLUMN thecolumn
TYPE TIMESTAMP WITH TIME ZONE
USING to_timestamp(thecolumn::timestamp with time zone);
You will note that I've used the type name "timestamp with time zone" instead of "datetime". That's because in PostgreSQL, datetime
is just an alias for timestamp without time zone
... but in most cases you actually want to use timestamp with time zone
instead. To learn more about timestamps, see the manual .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.