Bonjour, j'aimerais savoir comment connecter POSTGIS à Django avec Docker. Je rencontre une erreur could not open extension control file "/usr/share/postgresql/10/extension/postgis.control": No such file or directory
sortie
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/
Problème ici que vous n'utilisez pas l'image postgis correcte
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
Voici ma config Alpine pour travailler avec postgis
REMARQUE: tout mon code Django est stocké dans le dossier ./backend
et j'utilise Pipenv
, ainsi que Ce projet utilise https://github.com/joke2k/Django-environ pour définir DATABASES
via DATABASE_URL
dans mes paramètres
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
J'ai résolu ce problème sur Ubuntu 18.04 en suivant les instructions d'installation pour postgis détaillées ici: https://docs.djangoproject.com/fr/2.1/ref/contrib/gis/install/postgis/
En bref, exécutez: Sudo apt install postgis
.
Vous devrez peut-être également exécuter des commandes détaillées ici: https://docs.djangoproject.com/en/2.1/ref/contrib/gis/install/spatialite/
Je ne parle pas couramment dans le menu fixe, traduisez donc cette théorie en conséquence.