Postgresql – How to execute multiple postgres script from command prompt

postgresqlpostgresql-9.4psql

I have 100 script files which has insert scripts for the table. How can I execute these?

Scrip1.sql , script2.sql, script3.sql .... script100.sql

To execute single file, I am using the below command. But how can I execute all files?

psql -d DBName -p 5444 -U schemaname -f P_Scrip1.sql  2> err.log

Best Answer

If you don't need to provide a password, it's easy:

for FILE in `ls -1 *.sql`
do
  psql -d DBName -p 5444 -U schemaname -f $FILE  2>$FILE.err.log
done

If you need to provide a password, concatenate all of the files together, then run psql:

for FILE in `ls -1 *.sql`
do
  cat $FILE >> /tmp/allfiles.sql
done

psql -d DBName -p 5444 -U schemaname -f /tmp/allfiles.sql 2>allfiles.err.log

rm /tmp/allfiles.sql