将列类型从varchar更改为timestamp

时间:2014-11-04 15:22:22

标签: postgresql alter-table

我有一个像这样定义的表:

CREATE TABLE session_requests
(
  id character varying(255) NOT NULL,
  authorization_enc character varying(255),
  auto_close integer,
  date_created character varying(255) DEFAULT '1970-01-01 01:00:00'::character varying,,
....
)

我正在尝试

alter table session_requests alter column date_created type timestamp using date_created::timestamp;

我得到的错误是

ERROR:  default for column "date_created" cannot be cast automatically to type timestamp

有人有什么建议吗?

1 个答案:

答案 0 :(得分:6)

在一次交易中完成。您甚至可以在single statement

中执行此操作
ALTER TABLE session_requests
  ALTER date_created DROP DEFAULT
 ,ALTER date_created type timestamp USING date_created::timestamp
 ,ALTER date_created SET DEFAULT '1970-01-01 01:00:00'::timestamp;

SQL Fiddle.

除此之外:character varying(255)在Postgres中几乎总是一个糟糕的(毫无意义的)选择。更多: