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
asked Oct 17, 2016 at 12:32
-
Create one file that calls all the others.user1822– user18222016年10月17日 12:38:40 +00:00Commented Oct 17, 2016 at 12:38
2 Answers 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
answered Oct 17, 2016 at 12:39
-
i need to provide password ...asalthangam– asalthangam2016年10月17日 12:46:55 +00:00Commented Oct 17, 2016 at 12:46
-
@asalthangam: providing a password for that is a different question then "how do I run 100 SQL scripts" - see here for an answer to that: dba.stackexchange.com/questions/14740/…user1822– user18222016年10月17日 12:50:05 +00:00Commented Oct 17, 2016 at 12:50
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
answered Oct 17, 2016 at 12:44
-
file name is not unique . it is aa.sql , bb.sql ma.sql like thatasalthangam– asalthangam2016年10月17日 12:47:51 +00:00Commented Oct 17, 2016 at 12:47
-
I wrote the script based on the information on the question, based on your comment, my solution will not work. Phil answer will do the trick.Ahmad Abuhasna– Ahmad Abuhasna2016年10月17日 12:59:59 +00:00Commented Oct 17, 2016 at 12:59
lang-sql