How to execute multiple queries using psql command from bash shell?
-c
processes only one command. Without it however psql
expects commands to be passed into standard input, e.g.:
psql -U postgres -h <ip_addr> <database_name> << EOFSELECT * FROM xyz_table;SELECT * FROM abc_table;EOF
Or by using echo
and pipes.
at least from 9.6.2 this approach works as well:
psql -c "select now()" -c "select version()" -U postgres -h 127.0.0.1
now
2017-12-26 20:25:45.874935+01(1 row)
version
PostgreSQL 9.6.2 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.3.1-14ubuntu2) 5.3.1 20160413, 64-bit(1 row)
Using echo
and a pipe to fit it on a single line:
echo 'SELECT * FROM xyz_table; \n SELECT * FROM abc_table' | psql -U postgres