django - 预构建的 Django 项目在迁移时出错

标签 django postgresql manage.py django-manage.py

我有一个预先构建的 django 项目,需要在 ubuntu 18 上运行它。使用 postgresql 作为数据库引擎,完成并安装了所有先决条件。为项目创建了一个数据库并添加了具有所需权限的用户,然后将这些设置更新到 settings.py 文件

然后我运行了以下命令来准备迁移并实现它。

# python manage.py makemigrations
No changes detected

&

# python manage.py migrate
Traceback (most recent call last):
  File "/home/hsn/env_banax_api/lib/python3.6/site-packages/django/db/backends/utils.py", line 83, in _execute
    return self.cursor.execute(sql)
psycopg2.ProgrammingError: permission denied to create extension "postgis"
HINT:  Must be superuser to create this extension.


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

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/hsn/env_banax_api/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/hsn/env_banax_api/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/hsn/env_banax_api/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/hsn/env_banax_api/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/home/hsn/env_banax_api/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 77, in handle
    connection.prepare_database()
  File "/home/hsn/env_banax_api/lib/python3.6/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 26, in prepare_database
    cursor.execute("CREATE EXTENSION IF NOT EXISTS postgis")
  File "/home/hsn/env_banax_api/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/home/hsn/env_banax_api/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/hsn/env_banax_api/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/hsn/env_banax_api/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/hsn/env_banax_api/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/hsn/env_banax_api/lib/python3.6/site-packages/django/db/backends/utils.py", line 83, in _execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: permission denied to create extension "postgis"
HINT:  Must be superuser to create this extension.

你们中有人知道如何修复它吗? Django 服务器正在运行,但我得到的是这个页面,我想这与数据库连接有关。 附上当前显示的页面截图。

enter image description here

最佳答案

您可以做的是:使用postgres super 用户在您的数据库中创建postgis 扩展

  • 使用 super 用户名(您在安装 postgresql 时提供的用户名)登录到 postgres shell:

    sudo -i -u < super 用户用户名> psql

  • 连接到您的数据库:

    \connect <数据库名称>

  • 将 postgis 扩展创建为:

    创建扩展 postgis;

可以了解用户权限here如果您在创建扩展时遇到权限问题。

关于django - 预构建的 Django 项目在迁移时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56055805/

相关文章:

Django:如何将任意 html 属性添加到表单上的输入字段?

python - 使 CharField 在管理员中使用 PasswordInput

mysql - 如何将django项目(考勤系统)上传到centOS服务器

postgresql - 从PostgreSQL RDS实例发送通知(没有pg_notify)?

mysql - django - 生产中的 manage.py 错误

django - 收到错误<Follower : Follower object (None)> instance isn't saved. 使用bulk=False 或先保存对象

python - 我如何告诉 django-admin 使用哪个设置模块?

python - 如何解决 "iterator should return strings, not bytes"

java - Docker-compose up springboot + postgres 连接被拒绝

sql - 查询以获取每个数据库的日期时间