Rails + PostGIS错误迁移数据库
我跟随Daniel Azuma关于使用rails进行地理空间分析的讨论,但是每当我在第二个项目中运行rake db:migrate
时我都遇到了困难。
我的设置细节如下:我使用Postgres.app运行Postgresql ,它给出了Postgres的9.1.3版本和PostGIS的2.0.0版本。 我遇到了database.yml文件的一些问题,并运行了迁移。 (我添加了相关的gem,并在application.rb中要求他们的信息)
我的database.yml文件如下所示:
development: adapter: postgis postgis_extension: true host: localhost encoding: unicode database: my_app_development pool: 5 username: my_app password:
如果我添加以下行schema_search_path: "public,postgis"
我得到:
rake aborted! PG::Error: ERROR: schema "postgis" does not exist : SET search_path TO public,postgis
如果我删除该行,当我尝试迁移数据库时收到以下错误:
rake aborted! PG::Error: ERROR: relation "geometry_columns" does not exist LINE 1: SELECT * FROM geometry_columns WHERE f_table_name='schema_mi... ^ : SELECT * FROM geometry_columns WHERE f_table_name='schema_migrations'
有没有人知道如何解决这些问题?
在公共模式中删除PostGIS扩展并在postgis模式中重新创建它。
DROP EXTENSION PostGIS; CREATE SCHEMA postgis; CREATE EXTENSION PostGIS WITH SCHEMA postgis; GRANT ALL ON postgis.geometry_columns TO PUBLIC; GRANT ALL ON postgis.spatial_ref_sys TO PUBLIC
这是我如何解决这个问题。 我首先创建了一个新的迁移,将postgis添加到数据库中。 (我已经在mac上通过自制软件安装了postgis和postgresql。)
rails g migration add_postgis_to_database
在迁移文件中,我删除了更改方法,并使用execute方法添加POSTGIS。
execute("CREATE EXTENSION postgis;")
之后,您可以检查数据库以确保postgis可用。
psql your_database_name SELECT PostGIS_full_version();
您使用的是什么版本的PostgreSQL? EXTENSION
事件出现在9.1中 。 扩展是在一个包中加载多个对象的便捷方式。
如果你的数量小于9.1,你可能会按照这些指令 (所有-f
命令)加载PostGIS。 升级可能也是个好主意,但这取决于你。
实际上,install命令需要调用postgis版本
sudo apt-get install -y postgis postgresql-9.3-postgis-2.1
然后,最简单的方法是宣布
sudo -u postgres psql -c "CREATE EXTENSION postgis" your-pg-database-name
避免迁移打嗝。
确保您已安装此function
sudo apt-get install postgresql-9.3-postgis
由于错过了这个包,我面临同样的问题。
我遇到了同样的问题,除非@ Raido的解决方案解决了db:migrate的问题,我在创建租户时仍然遇到了Apartment gem的问题(例如在db:seed期间)。
我发现Rails自动将enable_extension "postgis"
添加到schema.rb,而Apartment用它来创建租户模式。 我不确定为什么公寓不使用现有的postgis扩展(可能是创建租户时search_path的问题),但这会导致相同的错误。
解决方案(如果你可以称之为)只是从schema.rb中删除enable_extension "postgis"
行。 此方法的唯一问题是,任何触发schema.rb刷新的后续迁移都会导致重新添加的行。
此外,我使用Apartment方法将postgis扩展添加到shared_extensions模式而不是自己的模式。 我的lib / tasks / db_extensions.rake看起来像:
namespace :db do desc 'Also create shared_extensions Schema' task :extensions => :environment do # Create Schema ActiveRecord::Base.connection.execute 'CREATE SCHEMA IF NOT EXISTS shared_extensions;' # Enable Hstore ActiveRecord::Base.connection.execute 'CREATE EXTENSION IF NOT EXISTS HSTORE SCHEMA shared_extensions;' # Enable uuid-ossp for uuid_generate_v1mc() ActiveRecord::Base.connection.execute 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp" SCHEMA shared_extensions;' # Enable postgis extension for geographic data types ActiveRecord::Base.connection.execute 'DROP EXTENSION IF EXISTS postgis;' ActiveRecord::Base.connection.execute 'CREATE EXTENSION postgis WITH SCHEMA shared_extensions;' ActiveRecord::Base.connection.execute 'GRANT USAGE ON SCHEMA shared_extensions to PUBLIC;' puts 'Created extensions' end end Rake::Task["db:create"].enhance do Rake::Task["db:extensions"].invoke end Rake::Task["db:test:purge"].enhance do Rake::Task["db:extensions"].invoke end
我的database.yml看起来像:
postgis_options: &postgis_options adapter: postgis postgis_extension: postgis # default is postgis postgis_schema: shared_extensions # default is public default: &default schema_search_path: 'public,shared_extensions' encoding: utf8 <<: *postgis_options ... production: <<: *default url: <%= ENV['DATABASE_URL'].try(:sub, /^postgres/, 'postgis') %>
不理想,但它正在发挥作用。 也许这将使用PostGIS和Apartment节省一两个小时。 我有兴趣知道是否有人比从schema.rb删除enable_extension
调用有更好的解决方案:)