Reputation: 333
I need to convert couple of columns in one table in PSQL and I really dont want to drop the table to fix this (thats my last resort). Is there a way to do this, because when I write:
ALTER TABLE table ALTER TABLE column type TIMESTAMP without time zone using column::TIMESTAMP without time zone;
it doesnt work, says:
ERROR:cannot cast type time without time zone to timestamp without time zone.
P.S. If its possible, I would like to avoid dropping columns because I already use indexes of those columns.
Upvotes: 8
Views: 17545
Reputation: 11
Postgresql will not directly allow to convert time without zone to timestamp without time zone.
You just need to convert first into time without time zone to character varying then change to character varying to timestamp without time zone
You can refer the following query
ALTER TABLE table_name
ALTER COLUMN colum_name TYPE character varying
USING column_name::character varying
ALTER TABLE table_name
ALTER COLUMN colum_name TYPE Timestamp without time zone
USING column_name::Timestamp without time zone
Now you will be able update successfully.
Upvotes: 1
Reputation: 125204
If you want the date part to be today:
alter table the_table
alter column the_column type timestamp without time zone
using current_date + the_column
Upvotes: 14