如何在 PostgreSQL 中将时间戳(0)设置为默认值而不是时间戳(6)?

Stb*_*Stb 5 postgresql configuration datetime timestamp

我正在使用存储时间序列数据的 PostgreSQL 数据库。数据使用“带时区的时间戳”类型存储,输出为 ISO 格式(postgresql 默认)。数据源是一个闭源软件,它创建具有“带时区的时间戳”数据类型的表。

根据 Postgres 文档,时间戳的精度为 6 位(微秒)。所以,我的时间序列每秒记录一次,我在输出中看到毫秒。

e.g. 2012-06-25 15:46:23.001
Run Code Online (Sandbox Code Playgroud)

绘制时间序列数据的软件以毫秒为单位变得疯狂,但我无法修改数据源软件以在创建表时使用时间戳(0)。

我的一位同事编写了一个函数,该函数查询整个数据库以将每个表中的时间戳数据类型更改为“timestamp(0) with time zone”。它运行良好,但它是一个手动操作,必须在每个新表创建后完成。我们想避免使用该功能。

是否可以通过编辑 postgresql.conf 告诉 postgresql 创建时间戳(0)时间戳数据类型而不是默认时间戳(6)?

Abe*_*sto 1

技巧(不要在生产数据库上执行它!):

\n\n
postgres=# alter type timestamptz rename to timestamptz6;\nALTER TYPE\npostgres=# create domain pg_catalog.timestamptz as timestamptz6(0);\nCREATE DOMAIN\npostgres=# create table t (tz1 timestamp with time zone, tz2 timestamptz);\nCREATE TABLE\npostgres=# insert into t values(now(), now());\nINSERT 0 1\npostgres=# select * from t;\n\xe2\x95\x94\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa4\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x97\n\xe2\x95\x91          tz1           \xe2\x94\x82          tz2           \xe2\x95\x91\n\xe2\x95\xa0\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa3\n\xe2\x95\x91 2016-08-11 21:06:17+03 \xe2\x94\x82 2016-08-11 21:06:17+03 \xe2\x95\x91\n\xe2\x95\x9a\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa7\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x9d\n(1 row)\n
Run Code Online (Sandbox Code Playgroud)\n\n

瞧!就是这样。

\n\n

现在让我们恢复之前的一切:

\n\n
postgres=# drop table t;\nDROP TABLE\npostgres=# drop domain pg_catalog.timestamptz;\nDROP DOMAIN\npostgres=# alter type timestamptz6 rename to timestamptz;\nALTER TYPE\n
Run Code Online (Sandbox Code Playgroud)\n\n

需要您自担风险使用它。还有祝你好运。

\n