在Heroku上启用Ruby PostGIS支持

5

我正在尝试在Heroku上启用我的Rails应用程序中的PostGIS。我的Gemfile包含activerecord-postgis-adapter宝石:

gem 'activerecord-postgis-adapter', '3.0.0'

但是,启动我的实例后,我没有看到完全支持已启用:

$ heroku run irb
Running `irb` attached to terminal... up, run.5549
irb(main):001:0> require 'rgeo'
=> true
irb(main):002:0> RGeo::Geos.supported?
=> false

我已添加heroku-geo-buildpack,如PostGIS article中所述,尽管我正在使用更新的、真正的多重构建包格式。
$ heroku buildpacks
=== staging Buildpack URLs
1. https://github.com/cyberdelia/heroku-geo-buildpack.git#1.3
2. https://github.com/heroku/heroku-buildpack-ruby.git#v140

我感到困惑,因为我的构建过程看起来是正确的:

-----> Multipack app detected
-----> Fetching custom git buildpack... done
-----> geos/gdal/proj app detected
       Using geos version: 3.4.2
       Using gdal version: 1.11.1
       Using proj version: 4.8.0_1
-----> Vendoring geo libraries done
-----> Fetching custom git buildpack... done
-----> Ruby app detected
-----> Compiling Ruby/Rails
...

我缺少什么?我没有设置BUILDPACK_URL环境变量,据我了解它是用于旧的多构建包方法的。

3
哦,这很尴尬。看起来我错过了Hobby层数据库不支持它的事实。那么问题就在于我了,虽然我本来期望会看到一些错误提示。 - pr1001
是的,只有生产层数据库。它们包括:标准-0 高级-0, 标准-2 高级-2, 标准-4 高级-4, 标准-5 高级-5, 标准-6 高级-6, 标准-7 高级-7 企业版-7, 企业版-8。 - droid-zilla
你一定要接受一个答案。我曾经遇到过同样的问题,解决方案一点也不明显。 - Stephen Corwin
2个回答

4

PostGIS可以与Heroku免费的dyno配合使用,并支持以下内容:

  • Rails 4.2
  • activerecord-postgis-adapter 3.1.4

您需要执行以下操作:

  1. 将config/database.yml设置为以下内容:

default: &default
  adapter: postgis
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5

development:
  <<: *default
  database: adopt_your_geek_development
  username: postgres
  host: mydb.com
  port: 9999
  postgis_extension: postgis
  schema_search_path: public,postgis

...

production:
  <<: *default
  database: appname_production
  username: appname
  password: <%= ENV['ADOPT_YOUR_GEEK_DATABASE_PASSWORD'] %>
  postgis_extension: postgis
  schema_search_path: public,postgis
  1. 添加构建包:

$ heroku buildpacks:add https://github.com/ddollar/heroku-buildpack-multi.git

以下是 .buildpacks 文件内容:

$ cat .buildpacks 
https://github.com/cyberdelia/heroku-geo-buildpack.git
https://github.com/heroku/heroku-buildpack-ruby.git
  1. 然后在config/environments/production.rb中添加一个小的Monkey Patch。
module ActiveRecord
   module ConnectionHandling
     class MergeAndResolveDefaultUrlConfig
       private
       def config
         @raw_config.dup.tap do |cfg|
           if url = ENV['DATABASE_URL']
             cfg[@env] ||= {}
             cfg[@env]["url"] ||= url.try(:gsub, "postgres", "postgis")
           end
         end
       end
     end
   end
end

我现在在Heroku的免费dyno和免费postgresql上工作良好。


我与Heroku的工作人员交谈过,他们推荐这个解决方案。 "PostGIS已经在公共测试版中发布,您可以在Hobby层次下尝试它。您可以从PostGIS:使用Rails和Django应用程序处理地理空间数据(https://devcenter.heroku.com/articles/postgis)中找到详细信息。" 它非常好用 :) - 让我每月节省50美元。 - evanjmg

0

我错过了Hobby级别的数据库不支持PostGIS的事实。


1
他们现在支持它。 - Pak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接