gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:13:09 26 4
gpt4 key购买 nike

我正在尝试获取当前的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com