I had the same issue, except while @Raido's solution fixed the issue for db:migrate, I was still having issues with the Apartment gem when a tenant was created (e.g. during db:seed).
I discovered that Rails was automagically adding enable_extension "postgis"
to schema.rb, which Apartment uses to create the tenant schema. I'm not sure exactly why Apartment doesn't use the existing postgis extension (perhaps an issue with the search_path at the time of tenant creation), but this results in the same error.
The solution (if you can call it that) was to simply remove the enable_extension "postgis"
line from schema.rb. The only problem with this approach is that any subsequent migrations which trigger a schema.rb refresh result in the line being re-added.
Also, I used the Apartment approach of adding the postgis extension to the shared_extensions schema instead of its own. My lib/tasks/db_extensions.rake looks like:
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 uuid-ossp for uuid_generate_v1mc()
ActiveRecord::Base.connection.execute 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp" SCHEMA shared_extensions;'
# Enable postgis extension for geographic data types
ActiveRecord::Base.connection.execute 'DROP EXTENSION IF EXISTS postgis;'
ActiveRecord::Base.connection.execute 'CREATE EXTENSION postgis WITH SCHEMA shared_extensions;'
ActiveRecord::Base.connection.execute 'GRANT USAGE ON SCHEMA shared_extensions to PUBLIC;'
puts 'Created extensions'
end
end
Rake::Task["db:create"].enhance do
Rake::Task["db:extensions"].invoke
end
Rake::Task["db:test:purge"].enhance do
Rake::Task["db:extensions"].invoke
end
And my database.yml looks like:
postgis_options: &postgis_options
adapter: postgis
postgis_extension: postgis # default is postgis
postgis_schema: shared_extensions # default is public
default: &default
schema_search_path: 'public,shared_extensions'
encoding: utf8
<<: *postgis_options
...
production:
<<: *default
url: <%= ENV['DATABASE_URL'].try(:sub, /^postgres/, 'postgis') %>
Not ideal, but it's working. Perhaps this will save someone an hour or two with PostGIS and Apartment. I'd be interested to know if anyone has a better solution than removing the enable_extension
call from schema.rb :)