Skip to content

Instantly share code, notes, and snippets.

@allolex
Forked from twik/install_postgis_osx.sh
Last active August 29, 2015 14:06
Show Gist options
  • Save allolex/6d0cd5afb5092eacca65 to your computer and use it in GitHub Desktop.
Save allolex/6d0cd5afb5092eacca65 to your computer and use it in GitHub Desktop.
# This file should be copy and pastable in bash or zsh.
# Homebrew uses this version as of this documentation's writing.
export POSTGRESQL_VERSION="2.1.3"
# 1. Install PostgreSQL postgis and postgres
brew install postgis
initdb /usr/local/var/postgres
## Start up the database. You can also use the launchctl method.
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
# 2. Create a template to be used for creating GIS-enabled databases
export POSTGIS_TEMPLATE="postgis_template"
createdb $POSTGIS_TEMPLATE
createlang plpgsql $POSTGIS_TEMPLATE
## Import Postgis Data
psql -d $POSTGIS_TEMPLATE -f /usr/local/Cellar/postgis/$POSTGRESQL_VERSION/share/postgis/postgis.sql
psql -d $POSTGIS_TEMPLATE -f /usr/local/Cellar/postgis/$POSTGRESQL_VERSION/share/postgis/spatial_ref_sys.sql
## Install raster support (optional)
psql -d $POSTGIS_TEMPLATE -f /usr/local/Cellar/postgis/$POSTGRESQL_VERSION/share/postgis/rtpostgis.sql
psql -d $POSTGIS_TEMPLATE -f /usr/local/Cellar/postgis/$POSTGRESQL_VERSION/share/postgis/topology.sql
## Test to see if it works
psql -d $POSTGIS_TEMPLATE -c "SELECT postgis_full_version();"
# You should see something like the following:
# postgis_full_version
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# POSTGIS="2.1.3 r12547" GEOS="3.4.2-CAPI-1.8.2 r3921" PROJ="Rel. 4.8.0, 6 March 2012" GDAL="GDAL 1.11.0, released 2014/04/16" LIBXML="2.9.1" LIBJSON="UNKNOWN" TOPOLOGY RASTER
# (1 row)
# 3. Set template permissions to $GIS_GROUP
export GIS_GROUP="gisgroup"
export YOUR_APP_USER="your_app_user" # $USER might be good for development work
export YOUR_APP_DB_NAME="your_app_db_name"
createuser -R -S -L -D -I $GIS_GROUP;
psql --set="gis_group=$GIS_GROUP --set="postgis_template=$POSTGIS_TEMPLATE" -d postgis_template
ALTER DATABASE :postgis_template OWNER TO :gisgroup;
ALTER TABLE geometry_columns OWNER TO :gisgroup;
ALTER TABLE spatial_ref_sys OWNER TO :gisgroup;
CREATE SCHEMA gis_schema AUTHORIZATION :gisgroup;
\q
#4. Add your app's user
createuser -i -l -S -R -d $YOUR_APP_USER
psql --set="appuser=$YOUR_APP_USER" --set="gis_group=$GIS_GROUP" -d postgres
GRANT :gis_group TO :appuser;
\q
#5. Create your app database from the postgis template you just set up
createdb -T $POSTGIS_TEMPLATE -O $YOUR_APP_USER $YOUR_APP_DB_NAME
# Some good references are:
# http://russbrooks.com/2010/11/25/install-postgresql-9-on-os-x
# http://www.paolocorti.net/2008/01/30/installing-postgis-on-ubuntu/
# http://postgis.refractions.net/documentation/manual-1.5/ch02.html#id2630392
# Some good references are:
# http://russbrooks.com/2010/11/25/install-postgresql-9-on-os-x
# http://www.paolocorti.net/2008/01/30/installing-postgis-on-ubuntu/
# http://postgis.refractions.net/documentation/manual-1.5/ch02.html#id2630392
#1. Install PostgreSQL postgis and postgres
sudo apt-get install postgresql-8.4 postgresql-client-8.4 postgresql-contrib-8.4
# Maybe you may need to install from this repository:
# https://launchpad.net/~ubuntugis/+archive/ubuntugis-unstable
sudo apt-get install postgresql-8.4-postgis
#2. Create a template to be used on creating GIS-enabled databases
sudo su postgres
createdb postgis_template
createlang plpgsql postgis_template
#Import Postgis Data
psql -d postgis_template -f /usr/share/postgresql/8.4/contrib/postgis-1.5/postgis.sql
psql -d postgis_template -f /usr/share/postgresql/8.4/contrib/postgis-1.5/spatial_ref_sys.sql
#Test if works
psql -d postgis_template -c "SELECT postgis_full_version();"
#3. Set template permissions to gisgroup
createuser -R -S -L -D -I gisgroup;
psql -d postgis_template
ALTER DATABASE postgis_template OWNER TO gisgroup;
ALTER TABLE geometry_columns OWNER TO gisgroup;
ALTER TABLE spatial_ref_sys OWNER TO gisgroup;
CREATE SCHEMA gis_schema AUTHORIZATION gisgroup;
\q
#4. Adds your app's user
createuser -i -l -S -R -d <app_user>
psql -d postgres
GRANT gisgroup TO <app_user>;
\q
# Note: Remember to adding the <app_user> to your /etc/postgresql/8.4/main/pg_hba.conf
# Example: local all <app_user> trust
#5. Create your app database
createdb -T postgis_template -O <app_user> <app_db>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment