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:

Table columns:

col1, col2, col3, fname, lname

CSV file contains values like:

col1,col2,col3
val1,val2,val3
val1,val2,val3

I want to add data from csv along with additional data i.e. col1,col2,col3,fname,lname in table using COPY functionality of postgres.

Can this be done?

share|improve this question

You can specify a list of columns in the copy command:

copy YourTable(col1,col2,col3) 
    from '/home/you/yourfile.csv' 
    delimiter ','
    csv header;

The header option tells copy to ignore the first row.

share|improve this answer
    
I need to add additional values. These values are not available in the CSV. – ketu b yesterday
    
You set the other columns after the copy with an update query – Andomar yesterday
    
Using copy only is not possible? – ketu b yesterday

I am using following workaround, I have created a shell script to prefix additional column values in the CSV file.

additional_cols="Fred,Solo";#fname,lname
csv_file="filename.csv";
sed -i -e "s/^/$additional_cols\,/" $csv_file

once the file is edited, its easy to import values using COPY of postgres. Use link to COPY documentation.

share|improve this answer

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.