PostGIS: Using Geospatial Data with Rails and Django Apps
Last updated 25 May 2018
PostGIS is available in public beta. The beta is available on all Production tier databases and currently supports PostGIS version 2.0. It is available on the Dev or Basic Hobby tier plans with Postgres version 9.4 and above. For Postgres 9.2 databases PostGIS is only available for those provisioned after April 20, 2013. 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.
$ pip install dj-database-url $ pip freeze > requirements.txt
Then add the following to the bottom of
import dj_database_url DATABASES['default'] = dj_database_url.config() DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis' GDAL_LIBRARY_PATH = os.getenv('GDAL_LIBRARY_PATH') GEOS_LIBRARY_PATH = os.getenv('GEOS_LIBRARY_PATH')
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.