Search code examples
djangodockergeodjango

How to connect postgis to django in docker


Hi I would like to know how to connect POSTGIS to django using Docker. I am having an error could not open extension control file "/usr/share/postgresql/10/extension/postgis.control": No such file or directory

output

root@localhost:~/try-geodjango# docker-compose run web python manage.py migrate
Starting try-geodjango_db_1 ... done
/usr/local/lib/python3.5/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
  """)
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
psycopg2.OperationalError: could not open extension control file "/usr/share/postgresql/10/extension/postgis.control": No such file or directory


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.5/site-packages/django/core/management/base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.5/site-packages/django/core/management/base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 81, in handle
    connection.prepare_database()
  File "/usr/local/lib/python3.5/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 25, in prepare_database
    cursor.execute("CREATE EXTENSION IF NOT EXISTS postgis")
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.5/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
django.db.utils.OperationalError: could not open extension control file "/usr/share/postgresql/10/extension/postgis.control": No such file or directory

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'db',
        'PORT': '5432',
        'CONN_MAX_AGE': 300
        }
}

docker-compose.yml

version: '3'

services:
  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
  db:
    image: mdillon/postgis:9.4
    environment:
      - POSTGRES_HOST=db
      - POSTGRES_PORT=5432
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password

Dockerfile

FROM evili/geodjango
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -U pip
RUN pip install -r requirements.txt
ADD . /code/

Solution

  • NOTE: There is an official image for postgis now https://hub.docker.com/r/postgis/postgis. Use that instead. Also in example below I am using alpine which is not recommended as python base due to issues with wheels (read more https://pythonspeed.com/articles/alpine-docker-python/)

    Problem here that you are not using correct postgis image

    version: '3'
    
    services:
      web:
        build: .
        command: python3 manage.py runserver 0.0.0.0:8000
        volumes:
          - .:/code
        ports:
          - "8000:8000"
        depends_on:
          - db
      db:
        image: mdillon/postgis:10 # <- here
        environment:
          - POSTGRES_HOST=db  # default postgres
          - POSTGRES_PASSWORD=password  # default postgres
          # all env vars bellow are redundant because they are same as default values
          # - POSTGRES_PORT=5432
          # - POSTGRES_NAME=postgres
          # - POSTGRES_USER=postgres
    

    Here is my alpine based config for working with postgis

    NOTE: all my django code is stored in ./backend folder and I use Pipenv, also this project is using https://github.com/joke2k/django-environ to set DATABASES via DATABASE_URL in my settings

    DATABASES = {
        'default': env.db(default='postgis://postgres:postgres@postgres:5432/postgres')
    }
    

    docker-compose.yml

    version: "3.7"
    
    
    services:
    
        postgres:
            image: mdillon/postgis:10-alpine
            volumes:
                - ./docker/postgres/:/docker-entrypoint-initdb.d/
                - postgres_data:/var/lib/postgresql/data
    
        backend:
            build:
                context: .
                dockerfile: docker/backend/Dockerfile
            ports:
                - "8000:8000"
            depends_on:
                - postgres
            volumes:
                - ./backend:/project/backend
    
    
    volumes:
        postgres_data:
    

    docker/backend/Dockerfile

    FROM python:3.6-alpine3.8
    
    # postgresql-client is required by psql
    # postgresql-dev musl-dev gcc are required by psycopg
    # NOTE: there is py3-psycopg2
    # libxml2-dev libxslt-dev are required by lxml
    # gdal-dev geos-dev proj4-dev are required by geodjango
    # libcrypto1.1 is required by gdal
    # NOTE: we actually need gdal-dev not gdal
    # linux-headers is required by uwsgi
    # gettext-dev is required by ./manage.py makemessages
    
    # TODO: optimize installation by using --virtual
    RUN apk update && apk upgrade \
        && apk add postgresql-client \
            postgresql-dev \
            musl-dev \
            gcc \
            libxml2-dev \
            libxslt-dev \
            linux-headers \
            gettext-dev \
        && apk add --repository http://dl-cdn.alpinelinux.org/alpine/edge/main \
            libcrypto1.1 \
        && apk add --repository http://dl-cdn.alpinelinux.org/alpine/edge/testing \
            gdal-dev \
            geos-dev \
            proj4-dev \
        && pip install pipenv
    
    ENV PYTHONUNBUFFERED=1 \
        PYTHONIOENCODING=UTF-8
    
    COPY docker/backend/docker-entrypoint.sh /
    ENTRYPOINT ["/docker-entrypoint.sh"]
    
    WORKDIR /project/backend
    
    COPY backend/Pipfile backend/Pipfile.lock /project/backend/
    
    RUN pipenv install --system --ignore-pipfile --dev
    

    docker/backend/docker-entrypoint.sh

    #!/bin/sh
    # NOTE: if there is no bash can cause
    # standard_init_linux.go:190: exec user process caused "no such file or directory"
    
    # https://docs.docker.com/compose/startup-order/
    set -euo pipefail
    
    WAIT_FOR_POSTGRES=${WAIT_FOR_POSTGRES:-true}
    
    if [[ "$WAIT_FOR_POSTGRES" = true ]]; then
        DATABASE_URL=${DATABASE_URL:-postgres://postgres:postgres@postgres:5432/postgres}
    
        # convert to connection string
        # https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING
        POSTGRES_URL=${DATABASE_URL%%\?*}
        # https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion
        POSTGRES_URL=${POSTGRES_URL/#postgis:/postgres:}
    
        # let postgres and other services (e.g. elasticsearch) to warm up...
        # https://www.caktusgroup.com/blog/2017/03/14/production-ready-dockerfile-your-python-django-app/
        until psql $POSTGRES_URL -c '\q'; do
            >&2 echo "Postgres is not available - sleeping"
            sleep 1
        done
        # >&2 echo "Postgres is up - executing command"
    fi
    
    if [[ $# -ge 1 ]]; then
        exec "$@"
    else
        echo "Applying migrations"
        python manage.py migrate --noinput -v 0
        echo "Generate translations"
        python manage.py compilemessages --locale ru -v 0
        echo "Starting server"
        exec python manage.py runserver 0.0.0.0:8000
    fi