Bash script to install PostgreSQL

automated-installbashpostgresql

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

Best Answer

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' }