Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"extra data after last expected column" while trying to import a csv file into postgresql

I try to copy the content of a CSV file into my postgresql db and I get this error "extra data after last expected column".

The content of my CSV is

    agency_id,agency_name,agency_url,agency_timezone,agency_lang,agency_phone 100,RATP (100),http://www.ratp.fr/,CET,, 

and my postgresql command is

COPY agency (agency_name, agency_url, agency_timezone) FROM 'myFile.txt' CSV HEADER DELIMITER ','; 

Here is my table

CREATE TABLE agency (     agency_id character varying,     agency_name character varying NOT NULL,     agency_url character varying NOT NULL,     agency_timezone character varying NOT NULL,     agency_lang character varying,     agency_phone character varying,     agency_fare_url character varying );       Column      |       Type        | Modifiers  -----------------+-------------------+-----------  agency_id       | character varying |   agency_name     | character varying | not null  agency_url      | character varying | not null  agency_timezone | character varying | not null  agency_lang     | character varying |   agency_phone    | character varying |   agency_fare_url | character varying |  
like image 671
Frederic Le Feurmou Avatar asked Nov 02 '14 16:11

Frederic Le Feurmou


People also ask

How do I import a CSV file into PostgreSQL using Python?

First, we import the psycopg2 package and establish a connection to a PostgreSQL database using the pyscopg2. connect() method. before importing a CSV file we need to create a table. In the example below, we created a table by executing the “create table” SQL command using the cursor.


1 Answers

Now you have 7 fields.

You need to map those 6 fields from the CSV into 6 fields into the table.

You cannot map only 3 fields from csv when you have it 6 like you do in:

\COPY agency (agency_name, agency_url, agency_timezone) FROM 'myFile.txt' CSV HEADER DELIMITER ','; 

All fields from the csv file need to to be mapped in the copy from command.

And since you defined csv , delimiter is default, you don't need to put it.

like image 127
Mladen Uzelac Avatar answered Sep 21 '22 19:09

Mladen Uzelac