Ruby on Rails+PostGIS遇postgis.control缺失错误,求解决方法
解决Rails+PostGIS执行
rake db:gis:setup时找不到postgis.control的问题 这个问题我之前踩过一模一样的坑!本质就是PostGIS扩展没有和你的PostgreSQL版本正确匹配安装,或者PostgreSQL服务没加载到扩展文件。下面是分系统的解决步骤,亲测有效:
第一步:确认PostGIS是否与PostgreSQL版本匹配安装
PostGIS是和PostgreSQL版本强绑定的,你装PG9.5就得装对应9.5版本的PostGIS,PG10对应10版本的,不能乱装。
对于Ubuntu/Debian系统:
- 先检查已安装的PostGIS版本:
如果输出里没有对应你PG版本的PostGIS(比如PG9.5对应apt list --installed | grep postgispostgis-9.5,PG10对应postgis-10),就执行安装命令:- PG9.5:
sudo apt-get install postgis-9.5 postgresql-9.5-postgis-2.4 - PG10:
sudo apt-get install postgis-10 postgresql-10-postgis-2.5
- PG9.5:
对于CentOS/RHEL系统:
- 确保已经添加了PostgreSQL官方源,然后安装对应版本的PostGIS:
- PG9.5:
sudo yum install postgis24_95 - PG10:
sudo yum install postgis25_10
- PG9.5:
第二步:重启PostgreSQL服务
安装完PostGIS后,必须重启PostgreSQL让它加载新的扩展文件:
sudo systemctl restart postgresql
第三步:检查Rails数据库配置与模板库
- 打开
config/database.yml,确认你的数据库配置里指定了PostGIS模板:development: adapter: postgis database: your_app_development username: postgres password: your_password template: template_postgis - 如果
template_postgis模板库不存在,需要手动创建:- 进入PostgreSQL命令行:
psql -U postgres - 依次执行以下SQL:
CREATE DATABASE template_postgis WITH TEMPLATE template0 ENCODING 'UTF8'; CREATE EXTENSION postgis; UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template_postgis'; - 退出psql:输入
\q回车
- 进入PostgreSQL命令行:
第四步:确认Rails相关Gem正确安装
检查你的Gemfile里是否包含以下两个Gem:
gem 'activerecord-postgis-adapter' gem 'rgeo-activerecord'
如果没有,添加后执行bundle install。
最后重试命令
完成以上步骤后,重新执行:
rake db:gis:setup
应该就能正常运行了!
内容的提问来源于stack exchange,提问作者midu




