I do not think that I will use plpgsql for this. A shell script could be much more useful:
#!/bin/sh DBHOST=mydbhost DBNAME=mydbname files=$1 target=$2 for file in ${files}; do psql -h ${DBHOST} ${DBNAME} -c "\copy ${target} FROM '${file}' delimiters ','" done
usage example:
csv2psql "$(ls *.out.csv)" someschema.tablename
Note. This way you will also get around the problem of reading files with COPY, which requires the postgres server user to have permission to read the file.
source share