Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

#!/bin/bash
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`" ]
        then
                executable=$1
                shift
                while [ -n "$1" ]
                do
                        DEBIAN_FRONTEND=noninteractive apt-get -q -y install "$1"
                        print_info "$1 installed for $executable"
                        shift
                done
        else
                print_warn "$2 already installed"
        fi
}

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 ./pgsql.sh 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 ./pgsql.sh 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
add comment

2 Answers 2

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
add comment

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
add comment

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.