0
0

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(Docker-Compose)

Last updated at Posted at 2023-01-22

Django-PostGIS(Docker-Compose)

自分用の勉強メモ

自分用 : Docker folder 12

実行環境構築

フォルダ構成

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

docker-compose.yml

docker-compose.yml
version: "3"
services:
  db:
      image: postgis/postgis:15-3.3
      ports: 
          - "5432"
      environment:
        - POSTGRES_DB=db
        - POSTGRES_USER=postgres
        - POSTGRES_PASSWORD=postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    links:
      - db
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

実行する

powershell
docker-compose run 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': 'db',
        'PORT': 5432,
    }
}

実行する

powershell
docker-compose up

確認する

http
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 環境を作成する

参考&引用:
クイックスタート・ガイド:Compose と Django
[docker] postgreSQL12 + PostGIS3 環境を作成する
postgreSQLへのshp fileのimport

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?