PostgreSQL - to_timestamp 未正确转换 unix 时间戳

标签 postgresql timestamp

我正在尝试获取当前的 UTC 时间,并将其插入到 PostgreSQL 时间戳中。但它不能正常工作。

我正在使用以下命令:

INSERT INTO public.rt_block_height 
VALUES(to_timestamp('2018-09-09 00:36:00.778653', 'yyyy-mm-dd hh24:mi:ss.MS.US'), 83.7)

但是,当我检查结果时,它看起来像这样:

tutorial=# select * from rt_block_height;
          time           | block_height
-------------------------+--------------
 2018-09-09 00:48:58.653 |         83.7
(1 row)

我不知道是什么导致了这种不匹配。

仅供引用,这是我的表格源代码:

CREATE TABLE IF NOT EXISTS public.rt_BLOCK_HEIGHT
(
"time" timestamp without time zone,
BLOCK_HEIGHT double precision
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE public.rt_BLOCK_HEIGHT
OWNER to postgres;
SELECT create_hypertable('rt_BLOCK_HEIGHT', 'time');

最佳答案

格式字符串中存在逻辑错误,因为您不应同时使用 MSUS。但是,您根本不需要该函数,只需将字符串转换为 timestamp:

INSERT INTO public.rt_block_height 
VALUES('2018-09-09 00:36:00.778653'::timestamp, 83.7)

来自 the documentation:

to_timestamp and to_date exist to handle input formats that cannot be converted by simple casting. For most standard date/time formats, simply casting the source string to the required data type works, and is much easier.

关于PostgreSQL - to_timestamp 未正确转换 unix 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52240475/

相关文章:

java - joda-time-2.2 将 LocalDateTime 转换为 java.sql.Timestamp

scala - 如何将时间戳列转换为纪元秒?

postgresql - 设计一个数据库来保存和查询一个动态范围?

postgresql - 分析: Why a query taking could take so long,好像成本不高?

python - 在 SQLAlchemy 中使用 PostgresQL INTERVAL,其中持续时间动态存储在数据库中并且不是参数

java - Java中字符串转换为时间戳时添加夏令时

.net - .NET 中的时间戳

postgresql - postgreSQL 中的显式类型转换

sql - 将唯一值插入数据库 PGSQL/MSSQL

timezone - 本地时区的 wget 文件时间戳