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