我有一个类型为 TIMESTAMP WITHOUT TIME ZONE
的列,并且希望将该列默认为 UTC 中的当前时间。获取 UTC 的当前时间很容易:
postgres=# select now() at time zone 'utc';
timezone
----------------------------
2013-05-17 12:52:51.337466
(1 row)
使用列的当前时间戳:
postgres=# create temporary table test(id int, ts timestamp without time zone default current_timestamp);
CREATE TABLE
postgres=# insert into test values (1) returning ts;
ts
----------------------------
2013-05-17 14:54:33.072725
(1 row)
但那使用本地时间。试图将其强制为 UTC 会导致语法错误:
postgres=# create temporary table test(id int, ts timestamp without time zone default now() at time zone 'utc');
ERROR: syntax error at or near "at"
LINE 1: ...int, ts timestamp without time zone default now() at time zo...
最佳答案
甚至不需要函数。只需在默认表达式两边加上括号:
create temporary table test(
id int,
ts timestamp without time zone default (now() at time zone 'utc')
);
关于postgresql - 使用 UTC 中的当前时间作为 PostgreSQL 中的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16609724/