pt12lol pt12lol - 3 months ago 12
Bash Question

Echo output from many PostgreSQL files

I am using PostgreSQL 9.5 on Fedora 24.

I've got the following files:

$ cat hello.sql
SELECT 'hello';
$ cat world.sql
SELECT 'world';


And there are two executions below:

$ psql samples -f hello.sql -f world.sql
?column?
----------
world
(1 row)

$ psql samples -f world.sql -f hello.sql
?column?
----------
hello
(1 row)


What should I do to see output from both scripts?

Answer

Mistake in my understanding was trying execute many PostgreSQL files in a single command. I should rather use the following bash script:

 $ cat hello_world.sh
SQL_FILES=(hello.sql world.sql)
for SQL_FILE in ${SQL_FILES[@]}
do
    psql samples -f $SQL_FILE
done

The execution below is exactly what I need:

 $ ./hello_world.sh
 ?column? 
----------
 hello
(1 row)

 ?column? 
----------
 world
(1 row)
Comments