3

Ive created some CSV files to bulk import into Postgres 9.2 with the COPY command, but I have a problem because one of the fields in the Postgres table is an array datatype.

Is it possible to import data with COPY into an array datatype, i have both integer and text arrays ?

CREATE TABLE artist (
    id                          integer NOT NULL,
    name                        text NOT NULL,
    realname            text,
    urls                        text[],
    namevariations      text[],
    aliases             text[],
    releases            integer[],
    profile             text,
    members             text[],
    groups                      text[],
        data_quality    text
);
4
  • 1
    In principle yes, but please post some data from your CSV file and any trials and errors. Commented Sep 16, 2015 at 8:54
  • I dont know what format to put the array data into ? Commented Sep 16, 2015 at 8:58
  • 1
    Which data type exactly? varchar[] or e.g. integer[]? Please edit your question and add the create table statement. Commented Sep 16, 2015 at 9:25
  • If you do not post more information, including some sample data, the best possible answer you will get on your question is "Yes". Commented Sep 16, 2015 at 9:26

2 Answers 2

4

Something like this should work:

id,data
1,{1,2,3}
2,{4,5,6}

This assumes numeric values.

If your data contains character values that in turn can contain the delimiter (a , in my example) you need to enclose each value in double quotes.

id,data
1,{foo,bar}
2,{"foo,bar", "bar,foo"}

The second row will put the strings 'foo,bar' and 'bar,foo' as two elements into the array


Edit

When using a comma as the delimiter, the array values must be quoted, e.g.:

id,data
1,"{1,2,3}"
2,"{4,5,6}"

When dealing with strings, you need to use single quotes inside the array:

id,data
1,"{foo,bar}"
2,"{'foo,bar', 'bar,foo'}"

Quoting is not necessary if you use a different delimiter, e.g. |

Sign up to request clarification or add additional context in comments.

3 Comments

are you sure there should be no quuotes or other delimeters around curly braces?
@a_horse_with_no_name "When using a comma as the delimiter, the array values must be quoted, e.g.:" You have to quote it AND escape the the internal commas. so for this to work you have to do 1, "{1\,2\,3}". I think you can avoid doing the internal quotes only if you specify WITH FORMAT CSV
For me it worked without doble quotes and with escaping internal commas for integer array like - 1,{1\,2\,3} Thanks @KevinVasko for mentioning about this escaping which helped me.
1

Do a test, e.g.:

create table test (id int, intarr int[], textarr text[]);
insert into test values
(1, array[1,2], array['a','b']);
copy test to 'c:\data\test.txt';

truncate test;
copy test from 'c:\data\test.txt';
select * from test;

and check the format of the file:

1   {1,2}   {a,b}

2 Comments

Thanks but this is using INSERT but I want to bulk load data using COPY
@PaulTaylor: what klin was trying to show you is how you can find out for yourself by running those statements and then looking at the generated file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.