0

I'm running 2 docker containers.

  1. Flask app - which has file volume attached.
  2. Postgres database which has a database volume attached.

I'm connecting to the Postgres container using Psycopg2 from Flask app. And I have a CSV file that I want to copy to the Postgres table. This CSV file is present in the volume attached to flask app. and the postgres container has no connection to this volume.

When I create a connection using psycopg2 and create a cursor and execute the copy command on that table. It is trying to copy from the folder that I have in Postgres container.

I tried \copy instead of copy for copying the data from the client container. But I'm getting Invalid syntax error. I even tried \\copy. That didn't work either.

Unable to process files due to exception: syntax error at or near "\" LINE 1: \copy account(username,password,email) from '/tmp/redshift11...

Underoos
  • 4,708
  • 8
  • 42
  • 85
  • `psql mydb -c "\copy data1 FROM 'data1.txt' WITH CSV HEADER DELIMITER AS ',' QUOTE AS '^'"` see https://stackoverflow.com/questions/8869067/problems-while-importing-a-txt-file-into-postgres-using-php/8869206#8869206 – LinPy Oct 21 '19 at 12:34
  • But that should be run on the terminal using `os.system()`. Correct me if I'm wrong. and I want to use `psycopg2` only for dealing with database operations. – Underoos Oct 21 '19 at 12:37
  • See the link I provided the copy is pg client command not default one – LinPy Oct 21 '19 at 12:41
  • That worked with `cursor.copy_expert()`. I just want to know if there will be any performance issues when using this. @LinPy – Underoos Oct 21 '19 at 13:22
  • sorry I have no answer for this question – LinPy Oct 21 '19 at 13:23
  • `\copy` is an internal command of psql, the interactive PostgreSQL terminal. It's a wrapper around `COPY ... FROM STDIN/TO STDOUT` that makes it possible to use local files with a remote database. That's why it was giving you an invalid syntax error. – fphilipe Oct 23 '19 at 11:50

0 Answers0