Alter a Column Data Type Postgres
·
Answer a question
Hey I have just started working on PostgreSQL, and I am wondering how can we change a column's data type, I tried the following command:
alter table tableName alter column columnName type timestamp with time zone;
However I got the following message:
column "columnName" cannot be cast to type timestamp with time zone
The current column's data type is int, and i would like to change it to timestamp
Answers
Postgres doesn't know how to translate int to timestamp. There are several cases and usually they have different starting date.
- Create temporary column with timestamp
- Update table and copy data from old column to temporary column using your own translation
- Drop old column
- Rename temporary column.
If you look into documentation, you will find one line syntax with example how to convert unix time integer type:
ALTER [ COLUMN ] column [ SET DATA ] TYPE type [ USING expression ]
更多推荐
所有评论(0)