4

I am writing the Content of a csv file to a PostgreSQL table with help of NPGSQL in version 3.2.5.

The content of my csv is the following

id, value
1, 89
2, 286
3, 80
4, 107

I use the following command to write

Using writer = conn.BeginTextImport("COPY tbl_test (id,value) FROM 'C:/temp/test.csv' DELIMITER ',' CSV HEADER")

When I run my code, the values are written into my database, but the command is throwing the following error message:

Received unexpected backend message CompletedResponse. Please file a bug.

When I run the command directly in the SQL Shell everything works fine, so the problem seems to be produced by NPGSQL.

Here is my command which I use in the SQL Shell:

\COPY tbl_test(id,value) FROM 'C:/temp/test.csv' DELIMITER ',' CSV HEADER;

Has anybody else experience with this message?

11
  • I think it's a good idea to add a few lines of the actual csv file. Commented Sep 20, 2017 at 7:02
  • Default delimiter is tab in text format and comma in csv, so maybe you need to specify that it is semicolon instead: DELIMITER ';' Commented Sep 20, 2017 at 7:42
  • I tried this already with the same result but I have edited my question therefore. Even I changed the delimiter from ';' to ',' Commented Sep 20, 2017 at 7:44
  • comparing your shell command and NPGSQL I assume you use psql in case of shell and remote COPY in case of NPGSQL - do you have this path on your server?.. C:/temp/test.csv?.. Commented Sep 20, 2017 at 7:55
  • Yes, the path exists and even via the NPGSQL command the datas are successfully stored, but with the difference that the command throughs an exeption as described above after the commit. Commented Sep 20, 2017 at 8:04

1 Answer 1

1

As answered in the github issue, You're using the API incorrectly.

If you have your CSV file on the client side (where the Npgsql app is running), then you should be using COPY tbl_test(value) FROM STDIN, not FROM c:\temp\test.csv. The latter is used when the csv file is on the PostgreSQL server. See the documentation.

If you simply want to import a file present on your server, just execute the COPY command as a regular SQL - create a command and execute it with ExecuteNonQuery. Don't use the BeginTextImport API.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the tip. Without using the API function TextImport it works. I am only a little bit confused, because there is no hint in the documentation of NPGSQL. In the docs you use TextImport so this was the reason why I used it too.
The sample code in the npgsql docs clearly shows an example with FROM STDIN, and with CSV data being written in C#. The PostgreSQL docs for COPY explain the difference between FROM STDIN and from a file present on the server.

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.