Installing Postgres Postgis for Ruby on Rails and Heroku

Posted on

Painful, yes?

Any time I setup a postgres instance to work with postgis, it never goes smoothly.

This guide here is meant to be the end all of searching over countless documentations on how to get it working on local & heroku.

The below process described was all gathered from numerous sources over the internet, github, postgis docs, rails docs and stackoverflow.


Open up your gemfile and put this:

gem 'activerecord-postgis-adapter'

then bundle install.

Put this into an initializer:

Perhaps the most important part of getting this all to work on heroku is this part here. Specifically rewriting the database url under production environment.

Run rails db:gis:setup. This adds the proper migrations to your migrations folder, and setups up everything postgis will need.

Run rails db:migrate. Once you migrate your database, you should be all set.


Adding a geometry field to your database is pretty simple. You’ll want to do something like this: rails g migration AddPointToModelName lonlat:st_point

. It doesn’t really matter what your column is called, as long as you specify :st_point as the data type.

Run your migrations to add the column to your database.

And in your code you can do something like this to save a new point:


Keep in mind that you have a few different geometry data types you can use, so not just an st_point. You’ll need to find out what those are on your own, as well as how to query on them.