How to Execute PostgreSQL Commands Inside Unix Shell Scripts
语法
psql DBNAME USERNAME << EOF
statement 1;
statement 2;
.
.
statement n;
EOF
PostgreSQL: Executing SQL from shell scripts
#!/bin/sh
dbname="test"
username="test"
psql $dbname $username << EOF
SELECT * FROM test;
EOF
PostgreSQL: Using variables in SQL from shell scripts
#!/bin/sh
dbname="test"
username="test"
wherecond="tgs"
psql $dbname $username << EOF
SELECT * FROM test WHERE col_name = '$wherecond';
EOF