call multiple Sql files in a single SQL file in Postgres
Solution 1:
If you are running these files through psql
you want the \i
directive ("execute commands from file").
xxx.sql:
\i aaa.sql
\i bbb.sql
\i ccc.sql
If you are passing these through some other program you will need to combine the files yourself - I do not believe there is any SQL-standard way of executing external files.
Solution 2:
Not exactly what you are asking for, but will serve your purpose: 1) Put all of your script files in a folder; and 2) use a bash script to iterate through your files and run psql. For example:
SCRIPTS_DIR=/home/myproject/scripts
DATABASE_NAME=database_name
for file in $SCRIPTS_DIR/*.sql
do sudo -u postgres psql $DATABASE_NAME -f $file
done
This is in fact a little better because you won't have to type your files' names.
Solution 3:
On a bash shell you can do it also with a simple find -exec
find sql/ -name *.sql -exec psql -U user -f {} \;