Search code examples
postgresqldistributed-databaseyugabytedb

How can I import a jsonb column from a csv file using the COPY command?


I am trying to import the following csv file into YugaByte DB YSQL. Note that the second entry in each row is a JSON object.

"15-06-2018","{\"file_name\": \"myfile1\", \"remote_ip\": \"X.X.X.X\"}"
"15-06-2018","{\"file_name\": \"myfile2\", \"remote_ip\": \"Y.Y.Y.Y\"}"

My table schema is:

postgres=# create table downloads_raw (request_date text, payload jsonb);

I want the JSON snippet in the imported file to become a JSONB value.

I tried doing the following:

postgres=# COPY downloads_raw FROM 'data.csv';

Hitting the following error:

ERROR:  22P04: missing data for column "payload"
CONTEXT:  COPY downloads_raw, line 1: ""15-06-2018","{\"file_name\": \"myfile1\", \"remote_ip\": \"X.X.X.X\"}""
LOCATION:  NextCopyFrom, copy.c:3443
Time: 2.439 ms

Solution

  • You need to specify FORMAT csv and ESCAPE '\'. Also, the format and escape options need to be enclosed in parenthesis. This should work:

    COPY downloads_raw FROM 'data.csv' WITH (FORMAT csv, ESCAPE '\');

    List of supported options for COPY command can be found here: https://docs.yugabyte.com/latest/api/ysql/commands/cmd_copy/