Glassfish部署应用无法读写Postgres13+PostGIS3.1.7几何字段问题
问题分析与解决方案
问题描述
执行以下SQL插入语句时,无法读写数据库的geometry字段:
INSERT INTO digital_addresses(gps_coordinates, digital_address) VALUES (ST_SetSRID(ST_MakePoint(?, ?), 4326), ?)
报错信息如下:
exception in get getOrSaveDigitalAddress. Reason: ERROR: function st_makepoint(real, real) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 233
已确认PostGIS已正确安装(pgAdmin中执行相同查询可成功),尝试过postgis-geometry-2.5.1.jar、postgis-jdbc-2.5.0.jar、postgresql-42.2.25.jar的不同版本组合,问题仍未解决。数据库环境信息:
执行select postgis_full_version();返回:
POSTGIS="3.1.7 aafe1ff" [EXTENSION] PGSQL="130" GEOS="3.9.2-CAPI-1.14.3" PROJ="8.2.1" LIBXML="2.9.7" LIBJSON="0.13.1" LIBPROTOBUF="1.3.0" WAGYU="0.5.0 (Internal)"
问题原因
- 驱动版本不兼容:你使用的
postgis-jdbc-2.5.x版本远低于数据库端的PostGIS 3.1.7,版本差异会导致驱动无法正确识别高版本PostGIS的函数签名,也无法处理新的空间类型映射逻辑。 - 参数类型不匹配:PostGIS的
ST_MakePoint默认接受double precision类型参数,但你的程序传入了real类型参数。pgAdmin能执行成功是因为它会自动隐式转换类型,但低版本的PostGIS JDBC驱动不会处理这种转换,导致数据库找不到匹配的函数。
适配版本
针对你的环境(PostgreSQL 13 + PostGIS 3.1.7),推荐使用以下驱动版本:
- PostgreSQL JDBC驱动:
postgresql-42.2.x至42.5.x版本均兼容PostgreSQL 13(你当前的42.2.25是可用的,但需搭配对应版本的PostGIS驱动) - PostGIS JDBC驱动:必须使用3.1.x系列版本,例如
postgis-jdbc-3.1.7.jar和postgis-geometry-3.1.7.jar,需保证PostGIS驱动版本与数据库端的PostGIS版本完全一致(至少大版本号匹配)。
解决步骤
- 移除
{domain_dir}/domain1/lib/ext目录下所有低版本的PostGIS驱动包 - 下载对应版本的
postgis-jdbc-3.1.7.jar和postgis-geometry-3.1.7.jar,放入上述目录 - 重启应用服务器
- (可选)若仍有问题,可在SQL语句中显式转换参数类型:
INSERT INTO digital_addresses(gps_coordinates, digital_address) VALUES (ST_SetSRID(ST_MakePoint(?::double precision, ?::double precision), 4326), ?)
内容的提问来源于stack exchange,提问作者Patrick Aboagye
相关产品推荐
相关产品推荐

