Skip to content

Instantly share code, notes, and snippets.

@springmeyer
Last active April 1, 2017 00:27
Show Gist options
  • Save springmeyer/f8bc66ea9e6731468a20 to your computer and use it in GitHub Desktop.
Save springmeyer/f8bc66ea9e6731468a20 to your computer and use it in GitHub Desktop.
Install postgres/postgis locally with mason (see also https://github.com/springmeyer/postgis-mason-install)
#!/usr/bin/env bash
set -eu
set -o pipefail
: '
Normally postgres is installed globally and you can only run one postgres server. This shows otherwise.
This script demonstrates how to:
- Install postgres/postgis locally
- Launch the machine on custom ports that will not conflict with a global postgres
- Test postgis functions
- Shutdown cleanly
This is fully isolated to the directory you are working in.
See also a version of this tested on travis: https://github.com/springmeyer/postgis-mason-install
'
GDAL_VERSION="2.1.3"
POSTGIS_VERSION="2.3.2"
function install_mason() {
mkdir -p ./mason
curl -sSfL https://github.com/mapbox/mason/archive/v0.9.0.tar.gz | tar --gunzip --extract --strip-components=1 --exclude="*md" --exclude="test*" --directory=./mason
}
function install_deps() {
./mason/mason install libgdal ${GDAL_VERSION}
./mason/mason install postgis ${POSTGIS_VERSION}
./mason/mason link postgis ${POSTGIS_VERSION}
}
function setup_local_config() {
# setup config
echo 'export CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"' > mason-config.env
echo 'export PGDATA=${CURRENT_DIR}/local-postgres' >> mason-config.env
echo 'export PGHOST=${CURRENT_DIR}/local-unix-socket' >> mason-config.env
echo 'export PGTEMP_DIR=${CURRENT_DIR}/local-tmp' >> mason-config.env
echo 'export PGPORT=1111' >> mason-config.env
echo 'export PATH=${CURRENT_DIR}/mason_packages/.link/bin/:${PATH}' >> mason-config.env
GDAL_DATA_VALUE=$(./mason/mason prefix libgdal ${GDAL_VERSION})/share/gdal/
echo "export GDAL_DATA=${GDAL_DATA_VALUE}" >> mason-config.env
}
function initialize_once() {
# do each time you use the local postgis
source mason-config.env
# do once: create directories to hold postgres data
#rm -rf ${PGHOST}
#rm -rf ${PGTEMP_DIR}
#rm -rf ${PGDATA}
mkdir -p ${PGHOST}
mkdir -p ${PGTEMP_DIR}
# do once: initialize local db cluster
./mason_packages/.link/bin/initdb
sleep 2
}
function start() {
# do each time you use the local postgis
source mason-config.env
# do each time you use this local postgis:
# start server and background (NOTE: hit return to fully background and get your prompt back)
./mason_packages/.link/bin/pg_ctl -w start -l postgres.log -o "-k $PGHOST"
}
function test() {
# do each time you use the local postgis
source mason-config.env
# set up postgres to know about local temp directory
./mason_packages/.link/bin/psql postgres -c "CREATE TABLESPACE temp_disk LOCATION '${PGTEMP_DIR}';"
./mason_packages/.link/bin/psql postgres -c "SET temp_tablespaces TO 'temp_disk';"
# add plpython support if you need
./mason_packages/.link/bin/psql postgres -c "CREATE PROCEDURAL LANGUAGE 'plpythonu' HANDLER plpython_call_handler;"
# create postgis enabled db
./mason_packages/.link/bin/createdb template_postgis -T postgres
./mason_packages/.link/bin/psql template_postgis -c "CREATE EXTENSION postgis;"
./mason_packages/.link/bin/psql template_postgis -c "SELECT PostGIS_Full_Version();"
./mason_packages/.link/bin/psql template_postgis -c "CREATE EXTENSION hstore"
}
function stop() {
# do each time you use the local postgis
source mason-config.env
# note: to stop db when you are done do:
./mason_packages/.link/bin/pg_ctl -w stop
}
function main() {
install_mason
install_deps
setup_local_config
initialize_once
start
test
stop
}
# for more usage tips see https://github.com/mapbox/mason/blob/master/scripts/postgis/2.3.2/test.sh
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment