使用\ COPY将带有JSON字段的CSV加载到Postgres [英] Using \COPY to load CSV with JSON fields into Postgres

查看:168
本文介绍了使用\ COPY将带有JSON字段的CSV加载到Postgres的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用\ COPY命令将TSV数据从文件加载到Postgres表中.

I'm attempting to load TSV data from a file into a Postgres table using the \COPY command.

这是示例数据行:

2017-11-22 23:00:00     "{\"id\":123,\"class\":101,\"level\":3}"

这是我正在使用的psql命令:

Here's the psql command I'm using:

\COPY bogus.test_table (timestamp, sample_json) FROM '/local/file.txt' DELIMITER E'\t'

这是我收到的错误:

ERROR:  invalid input syntax for type json
DETAIL:  Token "sample_json" is invalid.
CONTEXT:  JSON data, line 1: "{"sample_json...
COPY test_table, line 1, column sample_json: ""{\"id\":123,\"class\":101,\"level\":3}""

我验证了JSON的格式正确,并阅读了几个类似的问题,但是我仍然不确定这里发生了什么.一个解释太棒了

I verified the JSON is in the correct JSON format and read a couple similar questions, but I'm still not sure what's going on here. An explanation would be awesome

推荐答案

按原样加载数据文件:

\COPY bogus.test_table (timestamp, sample_json) FROM '/local/file.txt' CSV DELIMITER E'\t' QUOTE '"' ESCAPE '\'

这篇关于使用\ COPY将带有JSON字段的CSV加载到Postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆