Closed sshkarupa closed 6 years ago
I've found a solution for this problem. For some reason the postgis extension is install into public schema instead of shared_extensions
. In my case all iI need is to add another one command into the rake task:
# lib/task/db_enhancements.rake
namespace :db do
desc 'Creates shared_extensions Schema and enables postgis extension'
task extensions: :environment do
# drop PostGIS extension from public schema;
ActiveRecord::Base.connection.execute 'DROP EXTENSION IF EXISTS postgis;'
# Create Schema
ActiveRecord::Base.connection.execute 'CREATE SCHEMA IF NOT EXISTS shared_extensions;'
# Enable PostGIS
ActiveRecord::Base.connection.execute 'CREATE EXTENSION IF NOT EXISTS postgis SCHEMA shared_extensions;'
# Grant usage to public
ActiveRecord::Base.connection.execute 'GRANT usage ON SCHEMA shared_extensions to public;'
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
Now all works correctly
Hi @sshkarupa, A Question here, I Am having the same problem, I wrote your solution and I still have error . Are you help me, please?
@asergiop21 I've stumbled upon these many times on my servers.
The fix is to drop your db and re-create it with rake db:create
which works along with the aparment's db_enhancement task.
Steps to reproduce
I'm trying to enable postgis in a Apartment tenanted postgresql database and I'm bump the the following problem.
If I'm creating the database using postgresql adapter and then adding postgis configurations (adapter, schema_search_path, db_enhancements.rake) all works fine. But if I'm creating the new database using postgis adapter it doesn't work for creating a new tenant.
Expected behavior
Apartment::Tenant.create('my-tenant')
won't raise errors.Actual behavior
In
public
tenant everything fine.System configuration
Database: I'm using docker for development, so for postgresql i'm using the official image
postgresql:9.6-alpine
, for postgis -mdillon/postgis:9.6-alpine
accordingly.Database config (in
database.yml
):test: <<: *default database: <%= ENV.fetch('TEST_DATABASE_NAME', 'test_db') %>
lib/task/db_enhancements.rake
namespace :db do desc 'Creates shared_extensions Schema and enables postgis extension' task extensions: :environment do
Create Schema
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