Skip to content

Instantly share code, notes, and snippets.

@AndreyKozlov1984
Created June 17, 2016 19:36
Show Gist options
  • Save AndreyKozlov1984/45e63e6e8be002f054927aa4df915c3e to your computer and use it in GitHub Desktop.
Save AndreyKozlov1984/45e63e6e8be002f054927aa4df915c3e to your computer and use it in GitHub Desktop.
set -e
install_postgres () {
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
apt-get -y install wget ca-certificates
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
apt-get update
apt-get -y install \
postgresql-9.4 \
postgresql-server-dev-9.4 \
postgresql-contrib-9.4 \
libpq-dev
cat <<- EOF > /etc/postgresql/9.4/main/pg_hba.conf
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
EOF
chown postgres:postgres /etc/postgresql/9.4/main/pg_hba.conf
sudo -u postgres createuser vagrant -s || echo 'user exists'
sudo -u postgres bash -l -c 'psql -l | grep fpl || createdb fpl encoding=UTF8'
}
if [ ! -f /usr/lib/postgresql/9.4/bin/postgres ]; then
if [ ! -f /usr/lib/postgresql/9.1/bin/postgres ]; then
install_postgres
else #postgresql 9.1 is installed, need to upgrade it!
ls /tmp/upgrade_db.sql || (sudo -u postgres pg_dump fpl > /tmp/upgrade_db.sql && chown postgres:postgres /tmp/upgrade_db.sql)
service postgresql stop
sleep 5
apt-get -y remove --purge postgresql-9.1
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
apt-get -y install wget ca-certificates
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
apt-get update
install_postgres
sed -i 's/port = 5433.*/port = 5432/' /etc/postgresql/9.4/main/postgresql.conf
sudo service postgresql restart
sudo -u postgres psql fpl < /tmp/upgrade_db.sql
fi
fi
service postgresql restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment