我该如何在PostgreSQL中解决缺失的PostGIS SRIDs?

15

在运行我的Rspec测试套件时,我收到了以下错误:

PG::InternalError: ERROR:  GetProj4StringSPI: Cannot find SRID (4326) in spatial_ref_sys

我知道我启用了PostGIS扩展。我该怎么解决这个问题?

3个回答

38

问题在于某些东西从spatial_ref_sys表中删除了行。

在我的情况下,问题出现在spec_helper.rb中的DatabaseCleaner配置中。它被配置为删除/截断表格。

要防止这种行为,请更改配置。对我来说,做法是:

config.before(:suite) do
  DatabaseCleaner.strategy = :deletion, {:except => %w[spatial_ref_sys]}
  DatabaseCleaner.clean_with :truncation, {:except => %w[spatial_ref_sys]}
end

现在你需要重建那个表中的行。使用名为spatial_ref_sys.sql的脚本来执行此操作。

我使用的是Postgres.app,因此运行该脚本的命令是:

/Applications/Postgres.app/Contents/MacOS/bin/psql -d database_name -f /Applications/Postgres.app/Contents/MacOS/share/contrib/postgis-2.0/spatial_ref_sys.sql

您的命令可能略有不同。


1
macports postgis: /opt/local/share/postgresql93/contrib/postgis-2.1/spatial_ref_sys.sql - Seamus Abshere
4
我注意到只需重新运行 rake db:test:preparerake spec 即可修复 spacial_ref_sys 表的问题。 - Mike
该死,仍然无法在v1.5.1中使用 - 每次执行套件时都会删除spatial_ref_sys - lessless
这可能看起来很明显,但如果你定义了一个使用 delete 或截断的单独清理程序,你也需要在那里添加 {:except => %w[spatial_ref_sys]}。这对于 js 功能测试非常普遍,如 config.before(:each, :js => true)。即使功能测试没有使用 postgis,如果功能测试在其他测试之前运行,则在运行整个测试套件时仍会遇到相同的问题。 - Calvin
随着Postgres应用程序的更新版本,目录为/Applications/Postgres.app/Contents/Versions/9.4/share/postgresql/contrib/postgis-2.1/ - CWitty

14

运行 rake db:test:prepare 命令应该能够恢复 spatial_ref_sys 中的值。

更新:此问题已在版本1.4.1中得到修复:https://github.com/DatabaseCleaner/database_cleaner/pull/328

database_cleaner 版本1.4.0 存在一个bug, 因此需要指定表exceptions 的模式(schema):

DatabaseCleaner.strategy = :truncation, { except: ["public.spatial_ref_sys"] }
DatabaseCleaner.clean_with :truncation, { except: ["public.spatial_ref_sys"] }

在 1.4.0 版本中,模式(schema)作为表名的一部分返回。请参见https://github.com/DatabaseCleaner/database_cleaner/pull/282


4

这是因为缺少一个spatial_ref_sys表。请使用以下代码插入此表:

psql -d country -f /usr/share/postgresql/9.3/contrib/postgis-2.1/spatial_ref_sys.

country 是数据库名称,/usr/share/.../spatial_ref_sys.sql 是我的文件存储路径。这对我有效。


1
在我的Mac上,路径为/usr/local/Cellar/postgis/2.1.4_1/share/postgis/spatial_ref_sys.sql - baash05
Ubuntu: /usr/share/postgresql/12/contrib/postgis-2.5/spatial_ref_sys.sql - Kris
谢谢你的回答! - Sergey Miletskiy

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