0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Django-PostGIS-pgAdmin(Docker-Compose)

Last updated at Posted at 2023-01-22

Django-PostGIS-pgAdmin(Docker-Compose)

自分の勉強用備忘メモです。

(注意)利用しているツールについて、古いものがあります。

自分用 : Docker folder 13

実行環境構築

フォルダ構成

C:.
│  docker-compose.yml
│  Dockerfile
└─ requirements.txt

docker-compose.yml

docker-compose.yml
version: "3.9"
services:
  s_database:
      image: postgis/postgis:14-3.3
      container_name: c_postgis
      ports: 
        - 5432:5432
      volumes:
        - postgres_volume:/var/lib/postgresql/data
      environment:
        POSTGRES_DB: postgres
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: postgres
        POSTGRES_INITDB_ARGS: "--encoding=UTF-8"
      hostname: postgres
      restart: always
      user: root
  s_pgadmin:
      image: dpage/pgadmin4:5.0
      container_name: c_pgadmin4
      ports:
          - 8001:80
      volumes:
          - pgadmin4_volume:/var/lib/pgadmin
      environment:
          PGADMIN_DEFAULT_EMAIL: root
          PGADMIN_DEFAULT_PASSWORD: root
      hostname: pgadmin4
      depends_on:
        - s_database
      restart: always
  s_web:
    build: .
    container_name: c_django
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    links:
      - s_database
volumes:
  postgres_volume:
    external: true
  pgadmin4_volume:
    external: true
dockerfile
FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
requirements.txt
Django
psycopg2

Volumeの作成

powershell
docker volume create postgres_volume
docker volume create pgadmin4_volume

実行する

powershell
docker-compose run s_web django-admin.py startproject composeexample .

フォルダ構成

C:.
│  docker-compose.yml
│  Dockerfile
│  manage.py
│  requirements.txt
│  
└─composeexample
        settings.py
        settings.pyc
        urls.py
        urls.pyc
        wsgi.py
        wsgi.pyc
        __init__.py
        __init__.pyc

設定を修正する

\composeexample\settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 's_database',
        'PORT': 5432,
    }
}

実行する

powershell
docker-compose up

確認する

http://localhost:8000/

image.png

PostGISを有効化する

Databaseのコンテナに接続する

powershell
psql -h 127.0.0.1 -p 5432 -U postgres
sql
CREATE EXTENSION postgis;

テスト用のデータ作成

sql
CREATE TABLE test_gis (id serial primary key, name varchar(20), geom geometry(LINESTRING));

INSERT INTO test_gis (id, name, geom)
VALUES (
  1,
  'name1',
  ST_GeomFromText('LINESTRING(2 3,4 5,6 5,7 8)')
);

SELECT id, name, geom AS geom FROM test_gis;

引用元(一部加工編集):[docker] postgreSQL12 + PostGIS3 環境を作成する

pgAdminで確認

http://localhost:8001/

ターミナルで出力されているIPアドレスでデータベースに接続する。
pgadmin4 | ::ffff:XXX.XXX.XXX.XXX

image.png

参考&引用:
クイックスタート・ガイド:Compose と Django
[docker] postgreSQL12 + PostGIS3 環境を作成する
postgreSQLへのshp fileのimport
Docker for WindowsでPostgres+pgAdminのコンテナをたてる
pgAdmin4 v3.3+PostGISがすごい!
国土交通省にあるGISデータをPostGISへインポート(2022年版)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?