Home:ALL Converter>psql Loop through folder of CSVs to Copy to AWS RDS Postgres Table

psql Loop through folder of CSVs to Copy to AWS RDS Postgres Table

Ask Time:2018-02-19T23:04:56         Author:Frank B.

Json Formatter

I am trying to use a psql loop to load a directory of csv files into an AWS RDS Postgres database.

This StackExchange post answers how to do so locally and the following code works with a single file:

PGPASSWORD='MyPassword' psql -h my-rds.instance.us-east-1.rds.amazonaws.com -U user -d mydatabase -c '\COPY dbo.input_table FROM ''MySingleFile.csv'' CSV HEADER'

but I can't "combine" the two features. This code is the closest I can get but I get the following error for each file that is identified by the ls *.csv command:

for x in $(ls *.csv); 
do PGPASSWORD='MyPassword' psql -h my-rds.instance.us-east-1.rds.amazonaws.com -U user -d mydatabase -c "\COPY dbo.input_table FROM ''MySingleFile.csv'' CSV HEADER"; done

ERROR:  conflicting or redundant options
...
ERROR:  conflicting or redundant options

Author:Frank B.,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48868901/psql-loop-through-folder-of-csvs-to-copy-to-aws-rds-postgres-table
yy