postgresql - 如何更改 PostgreSQL 中的默认时间戳格式

标签 postgresql psql

<分区>

我是 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)

并且我也知道我可以在表定义中声明精度:

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 - 如何更改 PostgreSQL 中的默认时间戳格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47085187/

相关文章:

psql - 为什么我无法运行 "psql"命令?

c - 如何创建和迭代 graphids 列表(Apache AGE)?

sql - 加入倾斜关系时,有没有办法改进 PostgreSQL 估计?

sql - 如何在 bash 脚本中对每个命令进行计时(运行一系列 psql 语句)

postgresql - 替代 postgres 中用于脚本编写的列表命令

mysql - 如何在 PostgreSQL 中仅获取数据库的表名

ruby-on-rails - 在 RAILS 的 POSTGRESQL 数据库中存储 URL 的最佳数据类型是什么?

sql - 如何使用空字段执行查询?

postgresql - 创建 : could not connect to database template1: FATAL: password authentication failed for user

postgresql - 无法创建新的 Postgres 数据库