Search code examples
ruby-on-railspostgisgeoserver

ERROR: function postgis_lib_version() does not exist when matching a postgis custom schema


I have a rails app with a PostGIS database running in docker containners. I'm using apartment for multitenancy and activerecord-postgis-adapter to access the PostGIS geospatial database features from ActiveRecord. I have PostGIS installed in shared_extentions schema as the apartment docs suggest. When I try to configure a datastore in geoserver I get the following error:

Error creating data store, check the parameters. Error message: Unable to obtain connection: ERROR: function postgis_lib_version() does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts. Position: 8

Geoserver datastore connection parameters:

host: app-db
port: 5432
database: app_development
schema: shared_extensions
user: 
passwd:

database.yml:

default: &default
  adapter: postgis
  postgis_schema: shared_extensions
  schema_search_path: public, shared_extensions
  encoding: unicode
  database: app_development
  host: app-db
  username:
  password:

lib/tasks/db_enhancements.rake:

namespace :db do
  desc 'Also create shared_extensions Schema'
  task :extensions => :environment  do
    # Create Schema
    ActiveRecord::Base.connection.execute 'CREATE SCHEMA IF NOT EXISTS shared_extensions;'
    # Enable Hstore
    ActiveRecord::Base.connection.execute 'CREATE EXTENSION IF NOT EXISTS HSTORE SCHEMA shared_extensions;'
    # Enable Postgis
    ActiveRecord::Base.connection.execute 'CREATE EXTENSION IF NOT EXISTS postgis SCHEMA shared_extensions;'
    # Enable UUID-OSSP
    ActiveRecord::Base.connection.execute 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp" SCHEMA shared_extensions;'
    # Grant usage to public
    ActiveRecord::Base.connection.execute 'GRANT usage ON SCHEMA shared_extensions to public;'
  end
end

I've also tried it on pgAdmin with SELECT shared_extensions.postgis_version() and it works fine:

2.3 USE_GEOS=1 USE_PROJ=1 USE_STATS=1

But of course when I run SELECT postgis_version() I get:

ERROR:  function postgis_version() does not exist
LÍNEA 1: SELECT postgis_version()
                ^
SUGERENCIA:  No function matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 8

Solution

  • GeoServer expects PostGIS to be installed in the public schema does not prepend shared_extensions to it's postgis functions. If you want to do this then you need to add shared_extensions to the search path so that GeoServer can find the functions it will need.

    ALTER DATABASE mydb SET 'search_path' = public,shared_extension;
    

    See this note for details on how to move postgis to a different schema.