dan
dan

Reputation: 45622

Rails not fully compatible with postgresql+postgis geometry data type

I'm using Rails 3.1, and I have tables with the PostGIS geometry datatype. These don't seem to be compatible with rake db:schema:dump or rake db:test:clone and the rake test:* tasks. The tables containing this datatype are simply not processed and instantiated by these rake tasks.

Is there any patch or solution for this?

Upvotes: 4

Views: 995

Answers (1)

Oleksandr Skrypnyk
Oleksandr Skrypnyk

Reputation: 2820

There is a solution:

First of all, you need a PostgreSQL template with PostGIS functions support.

Create a template database:

$ psql -U postgres
> CREATE DATABASE template_postgis WITH TEMPLATE=template1 ENCODING='UTF8';
> \c template_postgis;
> CREATE LANGUAGE plpgsql;

Load necessary PostGIS functions into template (I'm using Homebrew, so find the paths to your PostGIS SQL files):

$ psql -f /usr/local/share/postgis/postgis.sql template_postgis
$ psql -f /usr/local/share/postgis/spatial_ref_sys.sql template_postgis

Set database as template and grant permissions:

$ psql -U postgres template_postgis
> UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template_postgis';
> GRANT ALL ON geometry_columns TO PUBLIC;
> GRANT ALL ON spatial_ref_sys TO PUBLIC;

Then, add gem 'postgis_adapter' to your Gemfile and run bundle. After that add template: template_postgis to your config/database.yml like this:

development:
  adapter: postgresql
  template: template_postgis
  database: postgis_db

And - voila! Welcome on board!

Upvotes: 4

Related Questions