简体   繁体   English

错误:在 psql 中使用 COPY FROM PROGRAM 时缺少列数据

[英]ERROR: missing data for column when using COPY FROM PROGRAM in psql

i'm trying to import the data from a cURL with the next command in psql:我正在尝试使用 psql 中的下一个命令从 cURL 导入数据:

COPY testtable FROM PROGRAM 'curl https://.....'

This is the data in the URL:这是 URL 中的数据:

[{"date":"20201006T120000Z","uri":"secret","val":"1765.756"},{"date":"20201006T120500Z","uri":"secret","val":"2015.09258"},{"date":"20201006T121000Z","uri":"secret","val":"2283.0885"}] [{"date":"20201006T120000Z","uri":"secret","val":"1765.756"},{"date":"20201006T120500Z","uri":"secret","val":"2015.09258 "},{"date":"20201006T121000Z","uri":"secret","val":"2283.0885"}]

But psql returns但是 psql 返回

ERROR: missing data for column "uri"错误:“uri”列缺少数据

I've tried copying it on tables with the columns as text and json format.我尝试将其复制到列为文本和 json 格式的表格上。 Also tried adding (DELIMITER ',') but that returns还尝试添加(DELIMITER ',')但返回

ERROR: extra data after last expected column错误:最后一个预期列之后的额外数据

i feel like the problem could be caused from the "[]" in the start and the end of the data, but i im not sure.我觉得问题可能是由数据开头和结尾的“[]”引起的,但我不确定。

These are the definitions of the tables that i used.这些是我使用的表的定义。

Table "public.test_table"表“public.test_table”

Column柱子 Type类型 Modifiers修饰符
date日期 text文本 not null不是 null
uri乌里 text文本
val text文本

Indexes: "test_table_pkey" PRIMARY KEY, btree (date)索引:“test_table_pkey”PRIMARY KEY,btree(日期)

Table "public.test_table2"表“public.test_table2”

Column柱子 Type类型 Modifiers修饰符
date日期 json json
uri乌里 json json
val json json

COPY only supports csv, text, and binary formats. COPY 仅支持 csv、文本和二进制格式。 It does not support JSON.它不支持 JSON。 It will import or export data into or out of json fields as a whole, but will not assemble or parse them.它将作为一个整体将数据导入或导出到 json 字段中或从中导出,但不会组装或解析它们。

You could use a staging table with one row and one column.您可以使用一行一列的临时表。

create temp table stage(x jsonb);

COPY stage FROM PROGRAM 'curl https://.....';

insert into test_table select f.* from stage,
   jsonb_populate_recordset(null::test_table, x) f;

If PostgreSQL offered a pg_read_program() function, you could use that directly rather than creating a stage table.如果 PostgreSQL 提供了 pg_read_program() function,您可以直接使用它而不是创建阶段表。 But it doesn't (but you could make one in C, or plpythonu or plperlu)但它没有(但你可以在 C 或 plpythonu 或 plperlu 中制作一个)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM