PostgreSQL Database psql Command Line Query

Sometime psql command line query can be useful when it comes to Bash script processing. There are data stored in PostgreSQL database and I would like to write a bash script to massage the data into report. Example below show query to PostgreSQL database by using the psql command.


/usr/local/bin/psql -c "SELECT email_address FROM members WHERE active is TRUE;" -U someuser -t -A -o member-email-address.txt accounting

psql is connecting to accounting database, query from members table and out the result into a txt file name member-email-address.txt.

-t – Return result only, do not result with table name.
-A – By default, returned result will have white space, this is to remove the white space.
-o – Output the result into a file.