2013-04-03 33 views
2

当我运行包含::geometry转换的PostgreSQL查询时,出现type "geometry" does not exist错误。我在Ubuntu 12.04上使用php5-pgsql V5.3.10,php5-fpm 5.4.13,Laravel 4,Postgresql 9.1,PostGIS 2.0.1。 geometry类型是PostGIS特定的。使用PHP时PostgreSQL查询错误

没有投射,查询运行良好。当使用pgAdmin3直接查询PostgreSQL数据库时,原始查询也可以正常工作。为什么是这样?

查询

$busstops = DB::connection('pgsql') 
    ->table('locations') 
    ->select(DB::raw('geog::geometry as lat, geog::geometry as lng')) 
    ->get(); 

查询而无需进行转换(没有错误)

$busstops = DB::connection('pgsql') 
    ->table('locations') 
    ->select(DB::raw('geog as lat, geog as lng')) 
    ->get(); 

错误:

Exception: SQLSTATE[42704]: Undefined object: 7 ERROR: type "geometry" does not exist LINE 1: select geog::geometry as lat from "locations" ^ (SQL: select geog::geometry as lat from "locations") (Bindings: array ( ))


\的dT几何

     List of data types 
Schema | Name |    Description 
--------+----------+----------------------------------------- 
public | geometry | postgis type: Planar spatial data type. 
(1 row) 
+0

你安装了'geometry'型成一个单独的模式这是对'search_path'中的任何机会手动查询,但不是通过你的工具?或者你不是真的在你的工具和手动查询中连接到同一个数据库? –

+0

@CraigRinger我跑了'CREATE EXTENSION postgis; CREATE EXTENSION postgis-topology;'连接到数据库时,我认为(不正确?)它会为所有模式安装'geometry'类型?我确实有多个模式,如何为specfic/all模式安装'geometry'类型? – Nyxynyx

+1

您不必为每个模式安装类型。您可以选择在自己的模式中安装扩展,在这种情况下,该模式必须位于search_path中,以使扩展可用。你似乎没有这样做,所以我怀疑这是一个search_path问题。在'psql'中用'\ dT geometry'确认,确认'geometry'类型被列为其中的模式。如果它被安装在'public'中,那么可能你的'search_path'被设置,以便它不包含您的应用使用“公共”模式吗? –

回答

2

应用程序被切换search_path周围以便public不上search_path。默认情况下,扩展名安装在public中,所以当您切换search_path时,您会发现geometry和其他PostGIS类型和功能在应用程序中不可用。

您需要:

  • 创建一个新的模式postgis;
  • 将PostGIS扩展插入到postgis模式;和
  • 确保新postgis模式是总是search_path,可能使用特定应用程序设置
+0

有没有办法确保某些模式*在搜索路径中总是*? – Ethereal