Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have a table in Postgres with only 2 columns

CREATE table dummy(col1 char(10), col2 char(10));

The CSV file has the following columns:

gid        prov             name        pop1996
4468       BC              VANCOUVER    514008
4501       BC               SURREY      304477
4473       BC               BURNABY     179209
4485       BC               RICHMOND    148867

How can I copy gid and name only from this file using \COPY?

share|improve this question
1  
Probably duplicate of stackoverflow.com/questions/12618232/… – Jan Marek Apr 21 at 11:59

If you don't mind creating other tables you can follow the Q/A @Jan Marek pointed out: http://stackoverflow.com/a/12619903/318174

If you can't create additional tables than you can do this:

You will need to preprocess the CSV file first by eliminating the columns you don't want. The easiest way is using UNIX cut.

cat file.csv | cut -d '\t' 1,3 > processed.csv

If your CSV file has some escaping needed you can use a python script I wrote a long time ago that is exactly like cut but respects CSV.

cat file.csv | csvcut.py -d '\t' 1,3 > processed.csv

Next you load up psql and enter:

\copy your_table (col1,col2) FROM '/path/to/processed.csv' DELIMITER '\t' CSV;
share|improve this answer
    
Is there a way of doing it with windows command prompt ? – Nidhi Apr 21 at 12:17
    
Python installs on windows so you can use my python script. – Adam Gent Apr 21 at 12:17
    
Oh you will still need to pipe the csv file to the python script. Well I recommend if you haven't already install Powershell, Cygwin, Babun or Gow. Once you do that open the better shell and run the command. – Adam Gent Apr 21 at 12:22
    
Is there a way out, If i don't want to install anything new ? – Nidhi Apr 21 at 12:36

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.