Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql COPY CSV ERROR: extra data after last expected column

I'm trying to import the data from http://www.unitedstateszipcodes.org/zip-code-database. A subset of the data looks like this:

"zip","type","primary_city","acceptable_cities","unacceptable_cities","state","county","timezone","area_codes","latitude","longitude","world_reg$
"00501","UNIQUE","Holtsville",,"I R S Service Center","NY","Suffolk County","America/New_York","631","40.81","-73.04","NA","US","0","384",
"00544","UNIQUE","Holtsville",,"Irs Service Center","NY","Suffolk County","America/New_York","631","40.81","-73.04","NA","US","0","0"

The postgresql command I running is this:

copy development.zip_codes FROM '/tmp/zip_code_database.csv' WITH DELIMITER ',' CSV HEADER;

And the result is this:

ERROR: extra data after last expected column
SQL state: 22P04
Context: COPY zip_codes, line 2: ""00501","UNIQUE","Holtsville",,"I R S Service Center","NY","Suffolk County","America/New_York","631"..."

What am I doing wrong with the import?

like image 720
Devin Dixon Avatar asked Nov 30 '13 14:11

Devin Dixon


People also ask

What is true for Copy command in PostgreSQL?

For COPY FROM , each field in the file is inserted, in order, into the specified column. Table columns not specified in the COPY FROM column list will receive their default values. COPY with a file name instructs the PostgreSQL server to directly read from or write to a file.

How do I add data to a CSV file in PgAdmin?

To import CSV using this PgAdmin Import CSV method, you have to do the following: Click on the Tools tab at the top of your PgAdmin Home Page. Select the Query Tool in the drop-down menu that appears. Enter the title and columns in your CSV file as an SQL Query.

How do I export a CSV file into PostgreSQL?

Import from CSV File to Postgresql by Command Line Using COPY Statement. We can make use of the PostgreSQL copy command to copy from the CSV file and import it to Postgres. We can import the data into the table with or without headers provided the CSV should be in the same format.


1 Answers

Works like a charm, here ...

DROP TABLE zipcodes CASCADE;
CREATE TABLE zipcodes
        ( id serial NOT NULL PRIMARY KEY
        , zzip varchar NOT NULL UNIQUE
        , ztype varchar
        , primary_city varchar
        , acceptable_cities varchar
        , unacceptable_cities varchar
        , state varchar
        , county varchar
        , ztimezone varchar
        , area_codes varchar
        , latitude varchar
        , longitude varchar
        , world_region varchar
        , country varchar
        , decommissioned varchar
        , estimated_population varchar
        , notes varchar
        ); 

COPY zipcodes (zzip,ztype,primary_city
     , acceptable_cities,unacceptable_cities
     , state,county,ztimezone,area_codes 
     , latitude,longitude,world_region,country
     , decommissioned,estimated_population,notes )
FROM '/tmp/zip_code_database.csv'
        WITH CSV HEADER delimiter ','
        ;

Result:

DROP TABLE
CREATE TABLE
COPY 42522

(maybe the OP has CR/CRLF problems ?)

like image 59
wildplasser Avatar answered Oct 12 '22 00:10

wildplasser