django - 无法在azure中使用postgres数据库部署django项目

标签 django postgresql azure azure-web-app-service azure-webapps

我知道这可能是一个新手问题,但我在这里被困了很长一段时间。

在我的 Azure 帐户中,我想在子域中部署 django-postgresql 项目。所以我做了以下步骤,

  • 使用 Linux 资源创建了一个 Python Web 应用
  • 使用相同的资源创建了一个 postgresql 服务器(单个服务器)
  • 开发中心设置 CI/CD
  • 同时在我的项目中设置 production.py 文件,并在我的应用服务的配置中设置这些变量值。
  • 我使用 azure CLI 在 postgresql 中成功创建了数据库(这是我在此过程中使用 CLI 的唯一步骤)
  • 然后我打开 SSH session 并激活 antenv。之后我运行了 python manage.py migrate 命令并收到以下错误....
  Traceback (most recent call last):
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/opt/python/3.8.6/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?


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

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/core/management/base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 92, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
    self.build_graph()
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/migrations/loader.py", line 216, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
    with self.connection.cursor() as cursor:
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
    return self._cursor()
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
    self.ensure_connection()
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/opt/python/3.8.6/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?

仅供引用,我没有使用 docker 和 azure CLI 来创建 web 应用程序。我仅使用 azure CLI 来创建数据库。

你能帮我解决这个问题吗?

谢谢。

最佳答案

作为错误消息,您的应用服务无法连接到服务器,您需要检查以下内容:

  1. 检查您是否已经像我一样设置了所有必需的应用程序设置: enter image description here

  2. 检查是否在 Azure Database for PostgreSQL 服务器的防火墙规则中启用允许访问 Azure 服务: enter image description here

如果您是新手,可以关注此 tutorial首先。

关于django - 无法在azure中使用postgres数据库部署django项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65869786/

相关文章:

python - django-bootstrap3 python包安装: no files found matching '*.png' under directory 'bootstrap3'

postgresql - 错误 : INTO specified more than once at or near "INTO"

windows - 将 Azure 网站迁移到 Azure 云服务

postgresql - flyway 使用现有数据库

php - 使用 PHP PDO 处理失败的 UNIQUE 约束?

ruby-on-rails - 在 Rails 中如何恢复表的索引?

azure - 配置 azure 搜索 IP 以连接到本地数据库

Django项目部署: cannot load static files

Django 社交授权 :Get email from linkedin, 推特和 Facebook

ajax - Django REST,ajax调用中突然出现403错误