Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy multiple CSV files into postgres

Tags:

postgresql

I am writing a SQL script to copy multiple .CSV files into a postgres database like this:

COPY product(title, department) from 'ys.csv' CSV HEADER;

I have multiple files I want to copy in. I don't want:

COPY product(title, department) from 'ys1.csv' CSV HEADER;
COPY product(title, department) from 'ys2.csv' CSV HEADER;
COPY product(title, department) from 'ys3.csv' CSV HEADER;
COPY product(title, department) from 'ys4.csv' CSV HEADER;
COPY product(title, department) from 'ys5.csv' CSV HEADER;

I would like to use a for loop for this instead of multiple copy commands. Is this possible? Thanks

like image 557
More Than Five Avatar asked Aug 30 '13 13:08

More Than Five


1 Answers

In linux pipe the output of the files listing to psql. Make copy use the standard input:

cat /path_to/ys*.csv | psql -c 'COPY product(title, department) from stdin CSV HEADER'

Look for the equivalent in other OSs

like image 189
Clodoaldo Neto Avatar answered Oct 15 '22 08:10

Clodoaldo Neto