PostgreSQL: How do I avoid unwanted characters in COPY command output? - postgresql

I have an array (text) (sample row) in my PostgreSQL 9.5 database like follows:
my_array(text)
1,112,292,19.7
I am exporting this text array using Postgres COPY command to a custom text file like this:
Copy
(
Select
my_array
from
my_table
Order by my_table_id
) to '~my_path/output.str' With DELIMITER ',';
I get the output:
1\,112\,292\,19.7\
How can I avoid these unwanted \ in my copy command output?

if the delimiter character (, in your case) is present in a string, it will be escaped (normally by prefixing it with a \)
If you use a different separator (from ,), the , separator doesn't have to be escaped.
If you quote the string in the output, the , separator doesn't have to be escaped.
-- CREATE TABLE my_table(my_table_id SERIAL PRIMARY KEY, my_array text);
-- INSERT INTO my_table(my_array )VALUES ('1,112,292,19.7') ;
COPY ( SELECT my_array FROM my_table ORDER BY my_table_id)
TO '/tmp/output.str'
WITH CSV DELIMITER ',' QUOTE '"'
;

Related

How to change a default separator for postgresql arrays?

I want to import csv with Postgres' arrays into a Postgres table.
This is my table:
create table dbo.countries (
id char(2) primary key,
name text not null,
elements text[]
CONSTRAINT const_dbo_countries_unique1 unique (id),
CONSTRAINT const_dbo_countries_unique2 unique (name)
);
and I want to insert into that a csv which looks like this:
AC,ac,{xx yy}
When I type copy dbo.mytable FROM '/home/file.csv' delimiter ',' csv; then the array is read as a one string: {"xx yy"}.
How to change a deafault separator for arrays from , to ?
You cannot to change array's separator symbol. You can read data to table, and later you can run a update on this table:
UPDATE dbo.countries
SET elements = string_to_array(elements[1], ' ')
WHERE strpos(elements[1], ' ') > 0;

How to escape one doulbe quote " in data file for psql \COPY import?

There are many standalone double quote '"' in a tab delimited text file, need to be loaded into PostgreSQL with psql \copy command.
If I use FORMAT CSV option, I have to specify the QUOTE, and QUOTE char needs to be paired.
Here is the code, and output,
create table t1(
c1 varchar(20),
n1 numeric
);
echo 'Alf_7" 5.12' > m.csv
psql> \copy t1 FROM 'm.csv' (FORMAT CSV, delimiter E'\t', NULL 'NULL', HEADER false);
ERROR: unterminated CSV quoted field
CONTEXT: COPY t1, line 1: "Alfa_7" 5.1
Use FORMAT text option. then you do not have to specify the QUOTE.
psql=> \copy t1 FROM 'm.csv' (FORMAT text, delimiter E'\t', NULL 'NULL', HEADER false);
COPY 1

psql create CSV with comma,quote and pipe as part of data

I am trying to create a CSV from table data.
column1 column2
this,is"part|of"the,:long,//data :"hello,
Expected output:
column1,column2
this\,is"part|of"the\,:long\,//data,:"hello\,
My end goal is to load this CSV delimited by ',' into a hive table
i tried this but didn't work:
\COPY (select * from table) TO /tmp/file.csv WITH (FORMAT CSV, DELIMITER ',', ESCAPE '\', HEADER TRUE);
CSV i got:
column1,column2
this,is"part|of"the,:long,//data,:"hello,
The commas weren't prefixed by backslash.
Any help is appreciated.

Can I use copy to add data into a Postgres array datatype

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
);
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. |
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}

How does postgres not to escape when I copy csv to it?

I want to copy a csv to postgres. And some value is string like this "{\"foo\": 123}"
If I use the COPY in the postgres directly, it will escape the string. when I select from postgres, it will become "{foo: 123}", but it's hard for me to handle, so how to not to escape the ". That is to say, I hope I can get the origin string "{\"foo\": 123}" when I select it from postgres
CREATE TABLE meuk
( bagger varchar
);
COPY meuk(bagger) FROM stdin WITH CSV QUOTE '"' ESCAPE E'\\' ;
"{\"foo\": 123}"
\.
SELECT * from meuk;
Result:
CREATE TABLE
bagger
--------------
{"foo": 123}
(1 row)