如何在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,但我不知道该怎么做。
答案 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