Home > Net >  Constant failure to import csv file via copy command in postgrsql
Constant failure to import csv file via copy command in postgrsql

Time:05-07

I have a file in my TEMP directory on a windows server

echo %TEMP%
C:\Users\BOB\AppData\Local\Temp\2

Below command to insert file to table:

psql -d BOBDB01 -c "\COPY "BOBTB01" FROM 'C:\Users\BOB\AppData\Local\Temp\2\file.csv' with csv";

This results in an error:

Password for user postgres: <pw_given>
ERROR:  relation "bobtb01" does not exist

It does exist though:

\d

 List of relations
 Schema |                              Name                               |   Type   | Owner
-------- ----------------------------------------------------------------- ---------- --------
 public | BOBTB01                                                        | table    | BOB

Can anyone please help me kick around some ideas as to why the copy command fails given that the table is there?

Thanks.

CodePudding user response:

"but it IS in double quotes...? no?"

No. The windows shell eats the double quotes, so PostgreSQL never sees them. You can get them though to PostgreSQL by either backwacking them, or doubling them. (backwacking them works in bash as well, doubling them is just a Windows-shell thing. If you are using MS PowerHell not cmd shell, then only doubling them works, backwacking does something different)

What currently happens is that that the unescaped " before B is regarded by the cmd shell as closing the very first " on the line, then you have the bare word BOBTB01, then the " after the 1 re-opens the quote, which extends to the rest of the line. The effect is that internal " are not passed on to the psql program.

  • Related