我有一个类型的列,TIMESTAMP WITHOUT TIME ZONE并希望将其默认设置为 UTC 中的当前时间。以 UTC 格式获取当前时间很容易:
TIMESTAMP WITHOUT TIME ZONE
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') );