1

I'm running django 3.2 on Ubuntu 22.04 LTS with the following database engine in settings.py:

'ENGINE' :'django.contrib.gis.db.backends.postgis'

My webserver runs through docker. This is my docker file :

FROM python:3.8.14

RUN apt-get -y update && apt-get -y upgrade && apt-get install -y ffmpeg 
RUN apt-get -y install libgdal-dev
RUN apt-get -y install postgis postgresql-postgis
COPY wait-for-it.sh /wait-for-it.sh

#Export GDAL lbrary path
# RUN export GDAL_LIBRARY_PATH = /usr/
# RUN export PATH = $PATH:$GDAL_LIBRARY_PATH
RUN export CPLUS_INCLUDE_PATH=/usr/include/gdal
RUN export C_INCLUDE_PATH=/usr/include/gdal

# Copy any files over
COPY entrypoint.sh /entrypoint.sh

# Copy any files over
COPY bootstrap_development_data.sh /bootstrap_development_data.sh

# Change permissions
RUN chmod +x /entrypoint.sh
RUN chmod +x /bootstrap_development_data.sh
RUN chmod +x /wait-for-it.sh
RUN groupadd -r docker && useradd -r -g docker earthling
RUN chown -R earthling /root/


ENTRYPOINT ["/entrypoint.sh"]

COPY requirements.txt /requirements.txt
RUN pip3 install --upgrade pip setuptools wheel
RUN pip3 install -r /requirements.txt
RUN yes | pip uninstall django-rq-scheduler
RUN yes | pip install -U django-rq-scheduler


VOLUME ["/opt/my-api"]

EXPOSE 80


CMD ["python", "manage.py", "runserver", "0.0.0.0:80"]

As you can see, I have already RUN :

RUN apt-get -y install postgis postgresql-postgis

while building my docker container. I have also checked whether postgis extension is installed by running the follwoing in my console

find /usr -name postgis.control -->>/usr/share/postgresql/14/extension/postgis.control

Despite the above steps, I get the follwoing trace of postgis extention not available:

my-api-webserver | 2023-03-14 12:53:47,869 django.db.backends DEBUG    (0.005) CREATE EXTENSION IF NOT EXISTS postgis; args=None
my-api-webserver | Traceback (most recent call last):
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 82, in _execute
my-api-webserver |     return self.cursor.execute(sql)
my-api-webserver | psycopg2.errors.FeatureNotSupported: extension "postgis" is not available
my-api-webserver | DETAIL:  Could not open extension control file "/usr/share/postgresql/15/extension/postgis.control": No such file or directory.
my-api-webserver | HINT:  The extension must first be installed on the system where PostgreSQL is running.
my-api-webserver | 
my-api-webserver | 
my-api-webserver | The above exception was the direct cause of the following exception:
my-api-webserver | 
my-api-webserver | Traceback (most recent call last):
my-api-webserver |   File "manage.py", line 25, in <module>
my-api-webserver |     execute_from_command_line(sys.argv)
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
my-api-webserver |     utility.execute()
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
my-api-webserver |     self.fetch_command(subcommand).run_from_argv(self.argv)
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
my-api-webserver |     self.execute(*args, **cmd_options)
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
my-api-webserver |     output = self.handle(*args, **options)
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
my-api-webserver |     res = handle_func(*args, **kwargs)
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 90, in handle
my-api-webserver |     connection.prepare_database()
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 26, in prepare_database
my-api-webserver |     cursor.execute("CREATE EXTENSION IF NOT EXISTS postgis")
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
my-api-webserver |     return super().execute(sql, params)
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/cacheops/transaction.py", line 98, in execute
my-api-webserver |     result = self._no_monkey.execute(self, sql, params)
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
my-api-webserver |     return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
my-api-webserver |     return executor(sql, params, many, context)
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
my-api-webserver |     return self.cursor.execute(sql, params)
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
my-api-webserver |     raise dj_exc_value.with_traceback(traceback) from exc_value
my-api-webserver |   File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 82, in _execute
my-api-webserver |     return self.cursor.execute(sql)
my-api-webserver | django.db.utils.NotSupportedError: extension "postgis" is not available
my-api-webserver | DETAIL:  Could not open extension control file "/usr/share/postgresql/15/extension/postgis.control": No such file or directory.
my-api-webserver | HINT:  The extension must first be installed on the system where PostgreSQL is running.
my-api-webserver | 
my-worker | 2023-03-14 12:53:49,932 I

Help appreciated !

Earthling
  • 83
  • 3
  • 13
  • Have you tried using an older version of postgresql? Seems possible that there would be some issues with using older version of django w/ latest postgres/postgis – Avocado Mar 21 '23 at 01:40
  • Sure. Will try that out. Currently using the latest version. – Earthling Mar 21 '23 at 03:20

1 Answers1

0

I encountered this problem too, but I found the error message

my-api-webserver | DETAIL:  Could not open extension control file "/usr/share/postgresql/15/extension/postgis.control": No such file or directory.
my-api-webserver | HINT:  The extension must first be installed on the system where PostgreSQL is running.

was NOT emitted from the django container, BUT from the postgres container. Thus, I solved this problem by installing postGIS extension in postgis container.

Also, postgis provides a docker image of postgres with postgis installed.

[Error message from postgres container]

  1. create docker network
docker network create postgres-net
  1. create postgres container
docker run --name postgres-host --network=postgres-net -e POSTGRES_PASSWORD=password -d postgres:15.2
  1. connect to postgres via psql
docker run -it --rm --network postgres-net postgres:15.2 psql -h postgres-host -U postgres

then enter password: password

  1. try to install postgis extension
postgres=# CREATE EXTENSION postgis;
>>> ERROR:  extension "postgis" is not available
>>> DETAIL:  Could not open extension control file "/usr/share/postgresql/15/extension/postgis.control": No such file or directory.
>>> HINT:  The extension must first be installed on the system where PostgreSQL is running.

[My docker-compose structure before solving the problem]

services:
  django:
    build: .
    ...(other settings omitted)...
  postgres:
    image: postgres
    ...(other settings omitted)...

[My docker-compose structure after solving the problem]

services:
  django:
    build: .
    ...(other settings omitted)...
  postgres:
-   image: postgres
+   image: postgis/postgis
    ...(other settings omitted)...
tsume-ha
  • 1
  • 3