Skip to content

Commit e63ee8f

Browse files
committed
Better check for PostGIS
Fixes #1400
1 parent c73c6f1 commit e63ee8f

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/pgsql.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,32 @@ get_postgresql_settings(pg_conn_t const &db_connection)
231231
return settings;
232232
}
233233

234+
static std::string get_database_name(pg_conn_t const &db_connection)
235+
{
236+
auto const res =
237+
db_connection.query(PGRES_TUPLES_OK, "SELECT current_catalog");
238+
239+
if (res.num_tuples() != 1) {
240+
throw std::runtime_error{
241+
"Database error: Can not access database name."};
242+
}
243+
244+
return res.get_value_as_string(0, 0);
245+
}
246+
234247
postgis_version get_postgis_version(pg_conn_t const &db_connection)
235248
{
236249
auto const res = db_connection.query(
237-
PGRES_TUPLES_OK,
238-
"SELECT regexp_split_to_table(postgis_lib_version(), '\\.')");
250+
PGRES_TUPLES_OK, "SELECT regexp_split_to_table(extversion, '\\.') FROM"
251+
" pg_extension WHERE extname='postgis'");
252+
253+
if (res.num_tuples() == 0) {
254+
throw std::runtime_error{
255+
"The postgis extension is not enabled on the database '{}'."
256+
" Are you using the correct database?"
257+
" Enable with 'CREATE EXTENSION postgis;'"_format(
258+
get_database_name(db_connection))};
259+
}
239260

240261
return {std::stoi(res.get_value_as_string(0, 0)),
241262
std::stoi(res.get_value_as_string(1, 0))};

0 commit comments

Comments
 (0)