我有一个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相关问答推荐

一列引用两个不同的表

为什么我的应用程序接收的是空值而不是布尔值?

如何删除Devtainer之前创建的PostgreSQL数据库?

在Postgres中,如何删除包含N个自然数的表,并替换为生成子查询?

将整数(以毫秒为单位的epoch时间)转换为PrimitiveDateTime

在Postgres中如何连接具有相同名称列的表并更改列名称?

在 postgres/presto/AWS-Athena 中,与 array_agg( (col1, col2) ) 相反的是什么来获得每组多行?

如何使用 PostgreSQL 数据库中的函数和存储过程从动态表中获取所有数据?参数传入的表名

使用 pgx.CopyFrom 将 csv 数据批量插入到 postgres 数据库中

如何从 .sql postgresql 备份恢复单个表?

如何在构建时链接 docker 容器?

MAC OS X 上的 Postgres 权限被拒绝

在函数中返回字段作为插入结果

如何判断每个组中是否存在值

安装时 postgres 的默认用户

获取 psycopg2 count(*) 结果数

无法在 postgresql hibernate 中使用名为user的表

在 Postgres 中显示关系、序列和函数的默认访问权限

学习 PL/pgSQL 的好资源?

PostgreSQL 9.1:如何连接数组中的行而不重复,加入另一个表