You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

已安装PostGIS但Django执行数据库迁移时仍抛出django.db.utils.NotSupportedError: extension 'postgis' not available错误

已安装PostGIS但Django执行数据库迁移时仍抛出django.db.utils.NotSupportedError: extension 'postgis' not available错误

我现在正尝试把Django项目的模型变更迁移到PostgreSQL 18.1服务器(x86_64-linux平台,gcc-11.4.0编译,64位),但每次执行迁移都会报错。具体来说,迁移流程在读取/home/[username]/[project]/[project_directory]/newenv/lib/python3.10/site-packages/django/db/backends/utils.py文件时,卡在return self.cursor.execute(sql)这一行抛出错误。

系统环境是WSL2的Linux:Linux DESKTOP-7C9U6H4 6.6.87.2-microsoft-standard-WSL2


报错相关的Django代码片段

迁移报错时触发的_execute函数代码如下:

def _execute(self, sql, params, *ignored_wrapper_args):
    # Raise a warning during app initialization (stored_app_configs is only
    # ever set during testing).
    if not apps.ready and not apps.stored_app_configs:
        warnings.warn(self.APPS_NOT_READY_WARNING_MSG, category=RuntimeWarning)
    self.db.validate_no_broken_transaction()
    with self.db.wrap_database_errors:
        if params is None:
            # params default might be backend specific.
            return self.cursor.execute(sql)
        else:
            return self.cursor.execute(sql, params)

项目的数据库配置

我的Django项目settings.py中数据库配置字典如下:

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': '[name]',
        'USER': '[user]',
        'PASSWORD': '',
        'HOST': '127.0.0.1',  # localhost
        'PORT': '5432',
    }
}

数据库端的操作与报错

我尝试直接在数据库(数据目录位于/usr/local/pgsql/data)中手动执行扩展安装命令:

CREATE EXTENSION postgis;

但得到如下错误提示:

ERROR: extension "postgis" is not available.
Hint: The extension must first be installed on the system where PostgreSQL is running.


已执行的安装操作

我已经在系统层面(虚拟环境外)执行了以下安装命令:

  • sudo apt install postgis
  • sudo apt install postgresql-18
  • sudo apt install ca-certificates gnupg
  • 添加PostgreSQL官方源:
    curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null
    sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
    

执行apt search postgis时,系统显示对应PostgreSQL 18的PostGIS包已经安装:

postgresql-18-postgis-3/jammy-pgdg-testing,jammy-pgdg,now 3.6.1+dfsg-1.pgdg22.04+1 amd64 [installed,automatic]
  Geographic objects support for PostgreSQL 18

postgresql-18-postgis-3-dbgsym/jammy-pgdg-testing,jammy-pgdg 3.6.1+dfsg-1.pgdg22.04+1 amd64
  debug symbols for postgresql-18-postgis-3

postgresql-18-postgis-3-scripts/jammy-pgdg-testing,jammy-pgdg,now 3.6.1+dfsg-1.pgdg22.04+1 all [installed,automatic]
  Geographic objects support for PostgreSQL 18 -- SQL scripts

现在的矛盾点是:明明apt显示已经安装了适配PostgreSQL 18的PostGIS扩展包,但数据库却始终找不到该扩展,进而导致Django迁移失败。有没有人遇到过类似情况,或者能指出问题出在哪里?

火山引擎 最新活动