Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert additional column values(not available in file) in postgresql table using COPY

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 using COPY? If not please suggest workaround.

like image 498
Ketu Avatar asked Nov 08 '22 22:11

Ketu


1 Answers

psql \COPY supports reading from program output. We can modify the file on the fly using cat and sed:

'cat myFile | sed \'s/$/;col1;col2/\''

like image 174
Balazs Gunics Avatar answered Nov 14 '22 22:11

Balazs Gunics