postgresql 将列从时区时间更改为时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18254917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
alter column from time with time zone to timestamp
提问by wilty
I am having trouble changing a column called end_datein a table called key_requestfrom time with time zone to timestampin my Postgres database . I have tried using the following code:
我在更改从 time with time zone 到我的 Postgres 数据库中调用end_date的表中调用的列时遇到问题。我尝试使用以下代码:key_requesttimestamp
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?
知道如何调整此查询以使其正常工作吗?
采纳答案by Declan_K
I woul do this in a series of steps
我将通过一系列步骤来做到这一点
- Alter the table, adding a new column
end_date1astime with time zone - Copy the date from
end_date(old) toend_date1 - Alter the table, droping the old
end_datecolumn - Alter the table,reaming
end_date1toend_date
- 更改表,添加一个新列
end_date1作为time with time zone - 将日期从
end_date(旧)复制到end_date1 - 改变表,删除旧
end_date列 - 修改表,扩孔
end_date1到end_date
回答by Roman Pekar
you can do something like this:
你可以做这样的事情:
alter table key_request
alter column end_date type timestamp with time zone using date('20130101') + end_date;
回答by DanielK
Changing from java.sql.Dateto java.util.Date
更改java.sql.Date为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/hibernate 方法的解决方案。
java.sql.Dateis translated todatejava.util.Dateis translated totimestamp without time zone
java.sql.Date被翻译成datejava.util.Date被翻译成timestamp without time zone

