Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to write a bash script to install PostgreSQL server on a Debian 6 VPS. I keep wiping the server and I'm like to write something to auto-install it.

export PATH=/bin:/usr/bin:/sbin:/usr/sbin

function print_info {
        echo -n -e '\e[1;36m'
        echo -n $1
        echo -e '\e[0m'

function print_warn {
        echo -n -e '\e[1;33m'
        echo -n $1
        echo -e '\e[0m'

function check_install {
        if [ -z "`which "$1" 2>/dev/null`" ]
                while [ -n "$1" ]
                        DEBIAN_FRONTEND=noninteractive apt-get -q -y install "$1"
                        print_info "$1 installed for $executable"
                print_warn "$2 already installed"

check_install postgresql postgresql
sudo -u postgres psql -c"ALTER user postgres WITH PASSWORD '$1'"
sudo service postgresql restart

However, this fails to run successfully.

root@server:~# bash -x ./ password
+ export PATH=/bin:/usr/bin:/sbin:/usr/sbin
+ PATH=/bin:/usr/bin:/sbin:/usr/sbin
+ check_install postgresql postgresql
++ which postgresql
+ '[' -z '' ']'
+ executable=postgresql
+ shift
+ '[' -n postgresql ']'
+ DEBIAN_FRONTEND=noninteractive
+ apt-get -q -y install postgresql
Reading package lists...
Building dependency tree...
Reading state information...
postgresql is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 7 not upgraded.
+ print_info 'postgresql installed for postgresql'
+ echo -n -e '\e[1;36m'
+ echo -n postgresql installed for postgresql
postgresql installed for postgresql+ echo -e '\e[0m'

+ shift
+ '[' -n '' ']'
+ sudo service postgresql start
+ sudo -u postgres psql '-cALTER user postgres WITH PASSWORD '\''password'\'''
perl: warning: Setting locale failed.
perl: warning: Falling back to the standard locale ("C").
could not change directory to "/root"
psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
+ sudo service postgresql restart
share|improve this question
Try running bash -x ./ password to get more information about what the script is doing. – Tom Shaw Sep 11 '11 at 4:09
@Tom Shaw, I updated the output. – Xeoncross Sep 12 '11 at 20:46

Your script works fine. I just executed it on clean debian wheezy and it worked fine. So the problem is not your script but that your SQL database is not running so you cannot make any query.

psql: could not connect to server: No such file or directory

Error about "no such file" is because you are sudoing in the root catalog to user which doesn't have permissions to be there. root@debian:~# sudo -u postgres ls ls: cannot open directory .: Permission denied root@debian:~# sudo -u postgres pwd /root

It's good to write scripts but you need to think of server as of cattle not pet. Installed postgresql with password is a state and because a script may fail for various reasons it can't be treated as reliable. When you install any automation tool you can easily configure server as you like and be sure that the result (state of server) will (almost) always be as you intended.

For example puppet would do that job with postgres module with this one liner:

class { 'postgresql::server': postgres_password => 'password' }

share|improve this answer

The error message is saying that it is unable to connect to the server; since you're not installing anything (in the output, postgres is already installed), it won't verify that it has successfully started up.

You may want to add the following:

check_install postgresql postgresql
sudo service postgresql start
sudo -u postgres psql -c"ALTER user postgres WITH PASSWORD '$1'"
sudo service postgresql restart

As Tom Shaw mentioned in his comments, you can add set -x to your script to have it output each and every line of your shell script before it runs it, so that you can see exactly what its doing. Furthermore, you can use set -e to have it exit if it ever encounters a non-zero value--which might help in the future; i.e., if postgres fails to install, it doesn't make sense to try restarting it, etc..

share|improve this answer
I added your line @Redmumba, and posted the output of bash -x. – Xeoncross Sep 12 '11 at 20:46
What does sudo service postgresql start output? – Andrew M. Sep 12 '11 at 22:41

Your Answer


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.