Skip to content

Instantly share code, notes, and snippets.

@loomsen
Created July 14, 2014 10:54
Show Gist options
  • Save loomsen/5f471d5927f116fdde24 to your computer and use it in GitHub Desktop.
Save loomsen/5f471d5927f116fdde24 to your computer and use it in GitHub Desktop.
#!/bin/bash
#PATH=/usr/local/bin:$PATH
if (( $EUID != 0 )); then
printf 'you need to run this script as root\n'
exit 1
fi
if ! grep -q 900913 /usr/share/proj/epsg; then
printf '<900913> +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs <>\n' >> /usr/share/proj/epsg
fi
if ! [[ -d /var/lib/pgsql/data/base ]]; then
printf 'postgresql not initialized, trying initdb...\n'
postgresql-setup initdb || { printf 'failed to initialize postgres db, bailing out\n'; exit 1; }
fi
if ! systemctl status postgresql.service >/dev/null; then
printf 'postgresql not running, starting it\n'
systemctl start postgresql || { printf 'failed to start postgres server, bailing out\n'; exit 1; }
fi
if ! sudo -u postgres psql -l | grep osm > /dev/null; then
printf 'creating database and user...'
sudo -u postgres /usr/bin/createuser --no-superuser --no-createrole --createdb osm
sudo -u postgres /usr/bin/createdb -T template0 -E UTF8 -O osm osm
sudo -u postgres /usr/bin/createlang plpgsql osm
sudo -u postgres /usr/bin/psql -U postgres -d osm -c "CREATE EXTENSION postgis;"
sudo -u postgres /usr/bin/psql -U postgres -d osm -c 'ALTER TABLE geometry_columns OWNER TO osm;'
sudo -u postgres /usr/bin/psql -U postgres -d osm -c 'ALTER TABLE spatial_ref_sys OWNER TO osm;'
# FIXME: change users password if desired
sudo -u postgres /usr/bin/psql -U postgres -d osm -c "ALTER USER osm WITH PASSWORD 'osm';"
printf 'adding user to pg_hba and restarting postgres...\n'
sed -e '/^local.*peer$/ilocal osm osm md5' /var/lib/pgsql/data/pg_hba.conf > /tmp/pg_hba.conf && \
mv /tmp/pg_hba.conf /var/lib/pgsql/data/pg_hba.conf
echo "host osm osm 127.0.0.1/32 md5" >> /var/lib/pgsql/data/pg_hba.conf
systemctl restart postgresql.service
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment