Skip to content

Instantly share code, notes, and snippets.

@un33k
Forked from klen/postgis install
Created November 21, 2011 20:44
Show Gist options
  • Save un33k/1383868 to your computer and use it in GitHub Desktop.
Save un33k/1383868 to your computer and use it in GitHub Desktop.
#!/bin/sh
POSTGISCONTRIB_PATH=`pg_config --sharedir`/contrib
POSTGIS=$(find $POSTGISCONTRIB_PATH -name 'postgis.sql')
POSTGIS_COMMENTS=$(find $POSTGISCONTRIB_PATH -name 'postgis_comments.sql')
POSTGIS_SPATIAL=$(find $POSTGISCONTRIB_PATH -name 'spatial_ref_sys.sql')
if [ -z "$POSTGIS" ] || [ -z "$POSTGIS_SPATIAL" ]; then
echo " * Not found postgis contrib files."
exit 1
fi
createdb -E UTF8 template_postgis # Create the template spatial database.
createlang -d template_postgis plpgsql # Adding PLPGSQL language support.
# psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';"
echo " * Install '$POSTGIS'."
psql -d template_postgis -f $POSTGIS
echo " * Install '$POSTGIS_COMMENTS'."
psql -d template_postgis -f $POSTGIS_COMMENTS
echo " * Install '$POSTGIS_SPATIAL'."
psql -d template_postgis -f $POSTGIS_SPATIAL
echo " * Grant all."
psql -d template_postgis -c "GRANT ALL ON geometry_columns TO PUBLIC;" # Enabling users to alter spatial tables.
psql -d template_postgis -c "GRANT ALL ON spatial_ref_sys TO PUBLIC;"
@un33k
Copy link
Author

un33k commented Nov 21, 2011

It might be useful to drop the database "if exists", before creating a new one.
This is specially good if, the scripts fails the first time, or someone using your scripts in order to recover a manual creation of the gis template. you can do it right after:

psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';"
psql -d template_postgis -c "DROP DATABASE IF EXISTS template_postgis
createdb -E UTF8 template_postgis # Create the template spatial database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment