如何更改 PostgreSQL 中的默认时间戳格式
How to change default timestamp format in PostgreSQL
我是 PostgreSQL 的新手,在一家只使用 MySQL 的公司工作了几年,我对 TIMESTAMP 类型有点措手不及:
CREATE TABLE example (
example_id SERIAL PRIMARY KEY,
time_utc TIMESTAMP
);
INSERT INTO example (time_utc) VALUES (NOW());
SELECT * FROM example;
example_id | time_utc
------------+----------------------------
1 | 2017-11-02 21:37:26.592814
我知道我可以简单地将字段转换为不太精确的形式:
SELECT example_id, time_utc::timestamp(0)
并且我也知道我可以在 table 定义中声明精度:
time_utc TIMESTAMP(0)
但是有没有办法将其更改为所有 TIMESTAMP
字段的默认精度?同样,有没有办法改变 NOW()
的行为以匹配这种格式(包括缺少时区)?
例如 MySQL:
SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2017-11-02 21:41:26 |
+---------------------+
PostgreSQL:
SELECT NOW();
now
-------------------------------
2017-11-02 21:42:48.855801+00
老实说,我只是想不出在过去的任何时候我希望 MySQL 的时间戳更精确,而不太精确的形式客观上更容易在眼睛上。这是一个简单的配置更改,还是我在过渡到 PostgreSQL 的过程中需要吸收和处理的东西?
谢谢
The date/time style can be selected by the user using the SET datestyle
command, the DateStyle parameter in the postgresql.conf
configuration file, or the PGDATESTYLE environment variable
on the
server or client.
https://www.postgresql.org/docs/current/static/datatype-datetime.html#datatype-datetime-output
可用的选项(掩码)也在同一参考中列出。但它们可能无法准确提供您想要的东西。除此之外,你还有 to_char() 函数
The formatting function to_char (see Section 9.8) is also available as
a more flexible way to format date/time output.
https://www.postgresql.org/docs/current/static/functions-formatting.html
我是 PostgreSQL 的新手,在一家只使用 MySQL 的公司工作了几年,我对 TIMESTAMP 类型有点措手不及:
CREATE TABLE example (
example_id SERIAL PRIMARY KEY,
time_utc TIMESTAMP
);
INSERT INTO example (time_utc) VALUES (NOW());
SELECT * FROM example;
example_id | time_utc
------------+----------------------------
1 | 2017-11-02 21:37:26.592814
我知道我可以简单地将字段转换为不太精确的形式:
SELECT example_id, time_utc::timestamp(0)
并且我也知道我可以在 table 定义中声明精度:
time_utc TIMESTAMP(0)
但是有没有办法将其更改为所有 TIMESTAMP
字段的默认精度?同样,有没有办法改变 NOW()
的行为以匹配这种格式(包括缺少时区)?
例如 MySQL:
SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2017-11-02 21:41:26 |
+---------------------+
PostgreSQL:
SELECT NOW();
now
-------------------------------
2017-11-02 21:42:48.855801+00
老实说,我只是想不出在过去的任何时候我希望 MySQL 的时间戳更精确,而不太精确的形式客观上更容易在眼睛上。这是一个简单的配置更改,还是我在过渡到 PostgreSQL 的过程中需要吸收和处理的东西?
谢谢
The date/time style can be selected by the user using the
SET datestyle
command, the DateStyle parameter in thepostgresql.conf
configuration file, or thePGDATESTYLE environment variable
on the server or client.
https://www.postgresql.org/docs/current/static/datatype-datetime.html#datatype-datetime-output
可用的选项(掩码)也在同一参考中列出。但它们可能无法准确提供您想要的东西。除此之外,你还有 to_char() 函数
The formatting function to_char (see Section 9.8) is also available as a more flexible way to format date/time output.
https://www.postgresql.org/docs/current/static/functions-formatting.html