0

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
asalthangam
  • 313
  • 1
  • 5
  • 8

2 Answers2

2

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 
Philᵀᴹ
  • 31,952
  • 10
  • 86
  • 108
0

if you are using Linux you can use the following script

for ((i=1;i<101;i++)); do psql -d DBName -p 5444 -U schemaname -f P_Scrip${i}.sql 2> err.log & done
Ahmad Abuhasna
  • 2,718
  • 4
  • 25
  • 36