Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 .

share|improve this question
3  
Isn't it telling you the problem: line 17: psql: command not found? You need to either specify the complete path to psql or install it if it doesn't exist. – devnull 11 hours ago
@devnull . but i have already installed psql – DEVANG PANDEY 11 hours ago
@DEVANGPANDEY can you show us the output for whereis psql ? – Antarus 11 hours ago
@devangpandey You need to add it to the PATH as well. Try issuing psql from the command line; you'll figure. – devnull 11 hours ago
@devnull In that case is my shell script correct? – DEVANG PANDEY 11 hours ago
show 2 more comments

1 Answer

up vote 0 down vote accepted

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 (i.e. 0600)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.