PostGIS: Using Geospatial Data with Rails and Django Apps
Last updated 04 March 2017
PostGIS is available in public beta. The beta is available on all Production tier databases and currently supports PostGIS version 2.0. It is not available on the Dev or Basic Hobby tier plans. PostGIS is only available with Postgres 9.2 databases provisioned after April 20, 2013 and all Postgres 9.3 and 9.4 databases. To enable PostGIS once connected to your PostgreSQL 9.2 database run:
CREATE EXTENSION postgis;
Setting up PostGIS with Rails
To use PostgreSQL as your database in Ruby applications you will need to include the
activerecord-postgis-adapter gem in your Gemfile.
To fully take advantage of PostGIS with Rails on Heroku you’ll need configure your app with a custom buildpack which includes the appropriate system dependencies. This buildpack includes that support. Be sure to deploy the buildpack before building any gems.
bundle install to download and resolve all dependencies. For more information on getting setup with
activerecord-postgis-adapter you can visit their docs.
Once you’ve installed the gem you will need to change the adapter to “postgis”. You can see how to modify your Rails connection adapter here.
Install the dj-database-url package using pip.
To fully take advantage of GeoDjango on Heroku you’ll need configure your app with a custom configuration which includes the appropriate system dependencies. This DevCenter article includes directions for enabling this support.
$ pip install dj-database-url $ pip freeze > requirements.txt
Then add the following to the bottom of settings.py:
import dj_database_url DATABASES['default'] = dj_database_url.config() DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'
This will parse the values of the DATABASE_URL environment variable and convert them to something Django can understand then set the engine to take advantage of PostGIS.