问题
I am having trouble changing a column called end_date
in a table called key_request
from time with time zone to timestamp
in my Postgres database . I have tried using the following code:
alter table key_request alter column end_date type timestamp with time zone using end_date::timestamp with time zone
I keep getting the following error:
ERROR: cannot cast type time with time zone to timestamp with time zone
Any idea of how I can adjust this query to work?
回答1:
I woul do this in a series of steps
- Alter the table, adding a new column
end_date1
astime with time zone
- Copy the date from
end_date
(old) toend_date1
- Alter the table, droping the old
end_date
column - Alter the table,reaming
end_date1
toend_date
回答2:
you can do something like this:
alter table key_request
alter column end_date type timestamp with time zone using date('20130101') + end_date;
sql fiddle demo
回答3:
Changing from java.sql.Date
to java.util.Date
ALTER TABLE key_request ALTER COLUMN end_date TYPE timestamp without time zone;
If you have dependant views that relate to that table
- drop views
- change column type
- recreate views
Solution coming from a java/hibernate approach while in type migration.
java.sql.Date
is translated todate
java.util.Date
is translated totimestamp without time zone
来源:https://stackoverflow.com/questions/18254917/alter-column-from-time-with-time-zone-to-timestamp