Postgres时间戳以unix时间(以毫秒为单位)作为bigint

时间:2014-09-22 15:59:07

标签: postgresql

如何在postgres中使用以下代码段:

ALTER TABLE mytable
ADD COLUMN create_time_utc bigint not null
DEFAULT (now() at time zone 'utc');

我希望新列create_time_utc是以毫秒为单位的unix时间(即自1970年1月1日Unix纪元以来的毫秒数)。

我知道我需要将postgres timestamp转换为bigint,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:14)

extract(epoch

alter table mytable
add column create_time_utc bigint not null
default (extract(epoch from now()) * 1000);

http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT