使用PHP时PostgreSQL查询错误

时间:2013-04-03 07:06:55

标签: postgresql postgis

当我运行包含::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特定的。

没有强制转换,查询运行正常。使用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)

1 个答案:

答案 0 :(得分:2)

应用程序正在切换search_path,以便public不在search_path上。默认情况下,扩展名已安装到public中,因此当您切换geometry时,您会发现search_path以及其他PostGIS类型和函数无法从应用程序中获取。

你需要:

  • 创建新架构postgis;
  • 将PostGIS扩展程序移动到postgis架构中;和
  • 确保postgis上的新search_path架构始终 ,可能使用特定于应用程序的设置
相关问题