1

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?

4
  • You could choose another delimiter(; or \t ) Commented Aug 26, 2017 at 15:00
  • Yeah. It worked! but I have other two columns (numeric) that need to be exported with this array. Thus, I would prefer COMMA as a delimiter. Commented Aug 26, 2017 at 15:05
  • You could try to add QUOTE "'"of '"' and/or force_quote(my_array) Commented Aug 26, 2017 at 15:13
  • Is this right? With DELIMITER ',' Where FORCE_QUOTE(my_array). Care to explain? Commented Aug 26, 2017 at 15:30

1 Answer 1

2
  • 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 '"'
        ;
Sign up to request clarification or add additional context in comments.

Comments

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.