Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I am trying to import a big data in a CSV file to a postgreSQL table. I found some guys saying following code would do that:

>CREATE TABLE mytable();
>COPY mytable FROM 'C:/myCSVfile.csv' DELIMITER ',' CSV;

However, this code invoked an error saying:

ERROR: extra data after last expected column
CONTEXT: COPY mytable, line 1: "Name, Gender, Age, Email, ... "

If I typed in

>COPY mytable FROM 'C:/myCSVfile.csv' DELIMITER '.' CSV HEADER;

instead, then I got following error:

ERROR: extra data after last expected column
CONTEXT: COPY mytable, line 2: "King Kong, M, 10, [email protected], ..."

I know that specifying columns like

>CREATE TABLE mytable(Name varchar(20), Gender char(1), Age int(2), Email varchar(40), ...);
>COPY mytable(Name varchar(20), Gender char(1), Age int(2), Email varchar(40), ...) FROM 'C:/myCSVfile.csv' DELIMITER ',' CSV HEADER;

would yield the postgreSQL's table in the way I want.

But I have a large number of columns (over 250) in the CSV file, so I need to import the data without specifying each columns as above.

Would somebody help me out?

Thanks in advance.

share|improve this question
2  
Unless you show the problem row(s), we'd need psychic powers to help you. Please show relevant rows and complete, unedited commands and errors. – Craig Ringer Jul 28 '14 at 8:55
    
COPY mytable FROM 'abolute/path/to/csvfile.csv' WITH CSV HEADER; is enough || note: the mytable filed(s) and CSV file header(s) should be same || – wingedpanther Jul 28 '14 at 8:56
    
and one more thing i want to know is, how you generating your *.CSV file ?? – wingedpanther Jul 28 '14 at 8:57
    
@CraigRinger I've just edit my questions. Would it be enough? – Louis Jul 28 '14 at 9:13
    
@dude The number of mytable's field(s)? You mean I need to set something up for the table? Since I have done nothing like that... All I did are just typing 'create command' and 'copy command' as above. I generated the CSV file using MS Excel. – Louis Jul 28 '14 at 9:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.