我在 Ubuntu 22.04 LTS 上运行 django 3.2,在 settings.py 中使用以下数据库引擎:
'ENGINE' :'django.contrib.gis.db.backends.postgis'
我的网络服务器通过 docker 运行。这是我的 docker 文件:
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"]
如你所见,我已经运行了:
RUN apt-get -y install postgis postgresql-postgis
在构建我的 docker 容器时。我还通过在我的控制台中运行以下命令来检查是否安装了 postgis 扩展
find /usr -name postgis.control -->>/usr/share/postgresql/14/extension/postgis.control
尽管执行了上述步骤,但我得到以下 postgis 扩展不可用的痕迹:
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
帮助赞赏!
我也遇到这个问题,但是找到了错误信息
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.
不是从 django 容器中发出的,而是从 postgres 容器中发出的。 因此,我通过在 postgis 容器中安装 postGIS 扩展来解决这个问题。
另外,postgis 提供了一个安装了 postgis 的 postgres 的 docker image。
[来自 postgres 容器的错误消息]
docker network create postgres-net
docker run --name postgres-host --network=postgres-net -e POSTGRES_PASSWORD=password -d postgres:15.2
docker run -it --rm --network postgres-net postgres:15.2 psql -h postgres-host -U postgres
然后输入密码:
password
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.
【解决问题前我的docker-compose结构】
services:
django:
build: .
...(other settings omitted)...
postgres:
image: postgres
...(other settings omitted)...
【解决问题后我的docker-compose结构】
services:
django:
build: .
...(other settings omitted)...
postgres:
- image: postgres
+ image: postgis/postgis
...(other settings omitted)...