简体   繁体   English

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

[英]How to set timestamp(0) as default instead of timestamp(6) in PostgreSQL?

I am working wih a PostgreSQL DB that stores time-series data. 我正在使用PostgreSQL DB来存储时间序列数据。 The data is stored using "timestamp with time zone" type and the output is ISO format (postgresql default). 使用“带时区的时间戳”类型存储数据,输出为ISO格式(默认为postgresql)。 The data source is a closed-source software that creates tables with "timestamp with time zone" data type. 数据源是一个闭源软件,可以创建具有“带时区的时间戳”数据类型的表。

The precision of the timestamp is till 6 digits (microseconds) according to Postgres documentation. 根据Postgres文档,时间戳的精度达到6位数(微秒)。 So, my time-series are recorded every second and I see milliseconds in the output. 所以,我的时间序列每秒记录一次,我看到输出中的毫秒数。

e.g. 2012-06-25 15:46:23.001

The software that plots the time-series data goes crazy with the milliseconds but I can't modify the data source software to use timestamp(0) when it creates the tables. 绘制时间序列数据的软件会以毫秒为单位疯狂但我无法修改数据源软件以在创建表时使用时间戳(0)。

A colleague of mine programmed a function that queries the whole database to alter the timestamp data type to "timestamp(0) with time zone" in every table. 我的一位同事编写了一个函数,该函数查询整个数据库,以便在每个表中将时间戳数据类型更改为“带时区的时间戳(0)”。 It works well but it's a manual action that has to be done after every new table is created. 它运行良好,但它是一个手动操作,必须在每个新表创建后完成。 We would like to avoid the use of the function. 我们希望避免使用该功能。

Is it possible to tell to postgresql to create timestamp(0) timestamp data types instead of the default timestamp(6) by editing postgresql.conf? 是否可以通过编辑postgresql.conf来告诉postgresql创建时间戳(0)时间戳数据类型而不是默认时间戳(6)?

This handling is very deeply hardcoded and cannot easily be changed by a user. 这种处理是非常难以编码的,并且用户不容易改变。 (See AdjustTimestampForTypmod() in the source code, if you are curious.) (如果您AdjustTimestampForTypmod()请参阅源代码中的AdjustTimestampForTypmod() 。)

You could create a view over the table that casts the timestamp column to timestamp(0) or formats it perhaps using to_char , and point your rendering software at that view. 您可以在表上创建一个视图,将timestamp列转换为timestamp(0)或使用to_char对其进行格式化,并将渲染软件指向该视图。

The trick ( do not perform it on the production DB! ): 诀窍( 不要在生产数据库上执行它! ):

postgres=# alter type timestamptz rename to timestamptz6;
ALTER TYPE
postgres=# create domain pg_catalog.timestamptz as timestamptz6(0);
CREATE DOMAIN
postgres=# create table t (tz1 timestamp with time zone, tz2 timestamptz);
CREATE TABLE
postgres=# insert into t values(now(), now());
INSERT 0 1
postgres=# select * from t;
╔════════════════════════╤════════════════════════╗
║          tz1           │          tz2           ║
╠════════════════════════╪════════════════════════╣
║ 2016-08-11 21:06:17+03 │ 2016-08-11 21:06:17+03 ║
╚════════════════════════╧════════════════════════╝
(1 row)

Voila! 瞧! Thats it. 而已。

Now lets restore everything as it was before: 现在让我们恢复以前的所有内容:

postgres=# drop table t;
DROP TABLE
postgres=# drop domain pg_catalog.timestamptz;
DROP DOMAIN
postgres=# alter type timestamptz6 rename to timestamptz;
ALTER TYPE

Use it at your own risk. 需要您自担风险使用它。 And good luck. 还有祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM