Skip to content
Advertisement

Import CSV file to Postgre

I’m writing some PHP code to import a CSV file to a Postgre DB, and I’m getting the error below. Can you help me?

Warning: pg_end_copy(): Query failed: ERROR: literal newline found in data HINT: Use “n” to represent newline. CONTEXT: COPY t_translation, line 2 in C:xampphtdocsimporting_csvimportcsv.php on line 21

JavaScript

Advertisement

Answer

You need to specify FILE_IGNORE_NEW_LINES flag in file() function as a 2nd parameter which otherwise by default will include the newline char at the end of the each array item. This is likely whats causing the issue here.

So just add this flag FILE_IGNORE_NEW_LINES so that lines extracted from csv file will not have newline char at the end of the each line:

JavaScript

Also I would recommend using fgetcsv() instead to read csv file.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement