我正在尝试获取当前的 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');
最佳答案
格式字符串中存在逻辑错误,因为您不应同时使用 MS
和 US
。但是,您根本不需要该函数,只需将字符串转换为 timestamp
:
INSERT INTO public.rt_block_height
VALUES('2018-09-09 00:36:00.778653'::timestamp, 83.7)
to_timestamp
andto_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/