Re: import CSV file to a table

From: John McKown <john(dot)archie(dot)mckown(at)gmail(dot)com>
To: Günce Kaya <guncekaya14(at)gmail(dot)com>
Cc: PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Re: import CSV file to a table
Date: 2017-03-08 13:55:36
Message-ID: CAAJSdjiJL4dNdr=92gufSCxFEpWZn_Mk_KC8+gAi7y-6=edb1Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Mar 8, 2017 at 3:13 AM, Günce Kaya <guncekaya14(at)gmail(dot)com> wrote:

> Hi all,
>
> I want to import content of CSV file to a table via bash script without
> creating temporary table and I also want to skip some columns in CSV file
> (for instance, CSV file has 12 column and main table has only 2 column, If
> possible I would use only 2 column in CSV file) Is there any way to do it?
>
> Regards,
>
> --
> Gunce Kaya
>

​Not too difficult, but "unusual". However, there is a restriction that the
data cannot have an embedded comma. That is, you can't have something like:
"a,b",c and want two columns with a,b and c as the values.

[tsh009(at)it-johnmckown-linux junk]$ cat ptest.csv
a,b,c,d,e,f,g,h,i
1,2,3,4,5,6,7,8,9
z,y,x,w,v,u,t,s,r,q

[tsh009(at)it-johnmckown-linux junk]$ cat ptest.sh
#!/bin/bash
printf "%s\n" 'COPY schema1.table1(column1from5, column2from7) FROM stdin;'
export IFS=','
while read i;do # read in the records until EOF
test -n "${i}" && { # ignore blank lines!
set ${i} # set shell variables $1, $2, ...
printf "%s\t%s\n" $5 $7 #write out columns 5 & 7
}
done
printf "%s\n" '\.' # write EOF delimiter for COPY

[tsh009(at)it-johnmckown-linux junk]$ ./ptest.sh <ptest.csv
COPY schema1.table1(column1from5, column2from7) FROM stdin;
e g
5 7
v t
\.

--
"Irrigation of the land with seawater desalinated by fusion power is
ancient. It's called 'rain'." -- Michael McClary, in alt.fusion

Maranatha! <><
John McKown

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Martijn Tonies (Upscene Productions) 2017-03-08 14:13:10 Re: import CSV file to a table
Previous Message Vick Khera 2017-03-08 13:02:57 Re: import CSV file to a table