Skip to content

Latest commit

 

History

History
42 lines (31 loc) · 616 Bytes

2016-08-08-psql在命令行中执行.md

File metadata and controls

42 lines (31 loc) · 616 Bytes

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