Search code examples
postgresqlcsvnpgsql

Writing the content of a csv file to PostgreSQL with NPGSQL fails


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?


Solution

  • 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.