postgis/postgis イメージがあるのでそれを使います。
このイメージは公式のpostgresイメージをベースに作成されていて、安心感があります。 Postgres の各バージョンと、PostGIS の各バージョンそれぞれの組み合わせから選べるので、多くの場面で使えるでしょう。
利用例
- Rails 6.0 + PostGIS 2.5 (activerecord-postgis-adapter)
docker-compose.yml
version: "3" volumes: pg_data: driver: local services: pg: image: postgis/postgis:11-2.5-alpine environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: password volumes: - pg_data:/var/lib/postgresql/data app: # 省略
Rails との統合
config/database.yml
adapter: postgis
で activerecord-postgis-adapter
を使います。
これは標準のアダプタに空間データとRubyオブジェクトに変換などの機能を追加するものです。
default: &default adapter: postgis encoding: unicode # For details on connection pooling, see Rails configuration guide # https://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> database: rails_postgis_sample_development username: <%= ENV.fetch("DB_USERNAME") { "postgres" } %> password: <%= ENV.fetch("DB_PASSWORD") { "password" } %> host: <%= ENV.fetch("DB_HOST") { "pg" } %> development: <<: *default test: <<: *default database: rails_postgis_sample_test production: <<: *default database: rails_postgis_sample_production
migration
enable_extension 'postgis'
class CreateExtensionPostgis < ActiveRecord::Migration[6.0] def up enable_extension 'postgis' unless extension_enabled?('postgis') end def down disable_extension 'postgis' if extension_enabled?('postgis') end end