0

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 .

7
  • 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. Commented Jul 16, 2013 at 7:10
  • @devnull . but i have already installed psql Commented Jul 16, 2013 at 7:14
  • @DEVANGPANDEY can you show us the output for whereis psql ? Commented Jul 16, 2013 at 7:15
  • @devangpandey You need to add it to the PATH as well. Try issuing psql from the command line; you'll figure. Commented Jul 16, 2013 at 7:16
  • @devnull In that case is my shell script correct? Commented Jul 16, 2013 at 7:23

1 Answer 1

0

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.