Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PSQL \copy from a bash script with QUOTE

I’m trying to insert data into some tables of a Postgres database with a bash script. I got it working like this

 psql -c '\copy raw.ap1_1 from file.csv with csv header;'

The problem is I wan’t to give the copy command the Quote option, but I can’t get it to work because of the quotes outside. I’ve tried wrapping single quotes in double quotes, double quotes in single quotes, using a backslash to escape quotes, etc.

What is want is something like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

psql -c '\copy raw.ap1_1 from file.csv with csv header quote as '"';'

But of course I can’t do that because the quotes are taken as part of the bash script. I either get a postgres syntax error, or I get a parsing EOF error (because the quotes override themselves).

>Solution :

This should work:

psql -c "\\copy raw.ap1_1 from file.csv (format 'csv', header, quote '\"')"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading