Question

My team and I are going through a maintenance phase currently, and I've been busy upgrading everything. Last week I moved the team from PostgreSQL 9.1 to 9.3 and PostGIS 1.5 to 2.1 which all went smoothly. Yesterday, the Rails team submitted a Rails 4 upgrade pull request that our CI swiftly rejected. Any suggestions would be greatly appreciated.

When attempting to create a new test database, I receive the following Rails error:

PG::RaiseException: ERROR:  PostGIS is already installed in schema 'public', uninstall it first : CREATE EXTENSION postgis SCHEMA public

Some specs:

  • Debian 7 Wheezy
  • PostgreSQL 9.3
  • PostGIS 2.1
  • Ruby 2.1.0
  • Rails 4

My PostgreSQL / PostGIS installation steps:

sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main" >> /etc/apt/sources.list'
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
apt-get update -y

apt-get install postgresql-9.3-postgis-2.1

sudo -u postgres psql -c "CREATE USER xxadmin WITH PASSWORD 'xxpass';"
sudo -u postgres psql -c "ALTER USER xxadmin WITH SUPERUSER;"

sudo -u postgres psql -c "create database template_postgis with template = template1;"
sudo -u postgres psql -c "UPDATE pg_database SET datistemplate = TRUE where datname = 'template_postgis';"
sudo -u postgres psql -d template_postgis -f /usr/share/postgresql/9.3/extension/postgis--2.1.2.sql
sudo -u postgres psql -d template_postgis -c "GRANT ALL ON geometry_columns TO xxadmin;"
sudo -u postgres psql -d template_postgis -c "GRANT ALL ON spatial_ref_sys TO xxadmin;"
sudo -u postgres psql -d template_postgis -c "VACUUM FREEZE;"
sudo -u postgres psql -c "ALTER DATABASE template_postgis OWNER TO xxadmin;"
sudo -u postgres psql -d template_postgis -c "ALTER VIEW geography_columns OWNER to xxadmin;"
sudo -u postgres psql -d template_postgis -c "ALTER TABLE geometry_columns OWNER to xxadmin;"
sudo -u postgres psql -d template_postgis -c "ALTER TABLE spatial_ref_sys OWNER to xxadmin;"
Was it helpful?

Solution

There are currently a few different methods to spatially enable a PostgreSQL database with PostGIS:

  1. Using the DDL command CREATE EXTENSION postgis;. Easiest and recommended.
  2. Using enabler scripts. This method has multiple steps, is useful if you have specific requirements, for example disallowing raster support.
  3. Using a template (e.g. template_postgis) database. This could be useful to setup multiple databases that have similar features. Sometimes template_postgis is pre-installed, other times you would need to create this.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top