简体   繁体   中英

Postgresql command execution in bash script

I have written a shell script to execute postgresql commands . Problem is when I am executing this script it is showing an error : line 17: psql: command not found

my script is as follows:

export PGPASSWORD=${PGPASSWORD-my_password}
echo "enter host"
read host
echo "enter database name"
read dbname
echo "enter username"
read username
psql -h $host $dbname $username <<EOF
SELECT * FROM test ; >>res.txt
EOF

( cat res.txt) | sed 's/;/<tab>/g' > $file.csv
rm res.txt
unset PGPASSWORD

Please suggest me what I am doing wrong .

Locate your psql binary with

which psql

you will get output like

/usr/bin/psql

Now you can use the complete path in your script or extend your $PATH

Just a short note! Don't save your password within the script. If you need the (psql) password for certain users you should save it in .pgpass

host:db:user:password

and give it the proper permission (ie 0600)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM