【问题标题】:django.db.utils.OperationalError Could not connect to serverdjango.db.utils.OperationalError 无法连接到服务器
【发布时间】:2015-07-08 08:43:56
【问题描述】:

我不知道如何解决这个问题

我不知道为什么我在尝试runserver 时收到此错误:

Performing system checks...

System check identified no issues (0 silenced).
Unhandled exception in thread started by <function wrapper at 0x1085589b0>
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 222, in wrapper
    fn(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 107, in inner_run
    self.check_migrations()
  File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 159, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 17, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/Library/Python/2.7/site-packages/django/db/migrations/loader.py", line 49, in __init__
    self.build_graph()
  File "/Library/Python/2.7/site-packages/django/db/migrations/loader.py", line 184, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/Library/Python/2.7/site-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
    self.ensure_schema()
  File "/Library/Python/2.7/site-packages/django/db/migrations/recorder.py", line 49, in ensure_schema
    if self.Migration._meta.db_table in self.connection.introspection.get_table_list(self.connection.cursor()):
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 165, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 138, in _cursor
    self.ensure_connection()
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
    self.connect()
  File "/Library/Python/2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
    self.connect()
  File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 122, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/Library/Python/2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 134, in get_new_connection
    return Database.connect(**conn_params)
  File "/Library/Python/2.7/site-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: could not connect to server: Connection refused
        Is the server running on host "127.0.0.1" and accepting
        TCP/IP connections on port 5432?

当我尝试连接到 postgres 时:

psql: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'beerad',
        'USER': 'bli1',
        'PASSWORD': '',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

【问题讨论】:

  • 你能给我们看看你的settings.py的内容(尤其是数据库部分)吗?

标签: python django postgresql


【解决方案1】:

这可能是一些问题:

  1. PostgreSQL 没有运行。检查它 sudo service postgresql status
  2. 您的 PostgresSQl 未在端口 5432 上运行。您可以输入 sudo netstat -nl | grep postgres

  3. 来检查它
  4. 您在尝试连接数据库时遇到了错误,例如用户名、密码或数据库名称。检查它们是否是 postgres 要求您连接它的内容,以及您要访问的 db_name。

  5. postgres 中的 postmaster.pid 问题。这可能是由于关机不当造成的。它可以提醒一个不允许您的服务器启动的 pid 活着。要修复它,您必须:

     * rm /usr/local/var/postgres/postmaster.pid 
     * pg_resetxlog -f /usr/local/var/postgres
    

    在此之后,如果你制作 postgres 的 runserver,它应该可以正常运行

Mac OSX 帮助:How to start PostgreSQL server on Mac OS X?

【讨论】:

  • 我测试了您提供的 2 个命令:bash: service: command not found netstat: lunt: unknown or uninstrumented protocol
  • 现在它已更新,它应该可以工作了。 Netstat 原生于 unix 系统
  • netstat: lnt: unknown or uninstrumented protocol =/
  • ok 试试 sudo nestat -nl,我觉得它不喜欢 -p v.V
  • 我得到了巨大的输出。我应该寻找什么具体的东西吗?
【解决方案2】:

尝试杀死所有 postgres 进程。我在 MAC 上,我在 ubuntus 论坛上找到的这个解决方案确实有效。

https://askubuntu.com/questions/547434/how-to-nicely-stop-all-postgres-processes

【讨论】:

    【解决方案3】:

    适用于 Windows

    转到搜索栏,只需输入“Open psql”并按 Enter。

    打开屏幕后,重新运行 django 项目。

    【讨论】:

      【解决方案4】:

      在我的情况下,一切都设置得很好,Postgres 有正确的端口,PostgreSQL 运行正常,但是 5432 端口与 phppgadmin 共享,我可以访问 phppgadmin,它让我可以访问 Postgres 数据库服务器,但是我的 Django 应用程序不工作,它会返回 Connection denied 错误。所以我将 phppgadmin 配置文件 (/etc/phppgadmin/config.inc.php) 上的端口号从 5432 更改为 5433,一切正常。

      【讨论】:

        【解决方案5】:

        在 MacOS 中,我根据 StackExchange 答案中给出的建议停止并重新启动了 postgresql: https://dba.stackexchange.com/a/171580/182403

        brew services stop postgresql
        rm /usr/local/var/postgres/postmaster.pid # adjust path accordingly to your install
        brew services start postgresql
        

        【讨论】:

          【解决方案6】:

          以下命令适用于我(Windows)-

          pg_ctl -D "C:\Program Files\PostgreSQL\11\data" restart
          

          然后再次运行服务器-

          python manage.py runserver
          

          【讨论】:

            【解决方案7】:

            重启服务器解决了我这边的问题。

            【讨论】:

              【解决方案8】:

              如果您在使用 Google Cloud 中的 PostgresSQL 时看到此错误,请遵循中提到的所有配置 https://cloud.google.com/python/django/flexible-environment#macos-64-bit

              还分开HOST配置如下

              DATABASES['default']['HOST'] = '/cloudsql/' 如果 os.getenv('GAE_INSTANCE'): 经过 别的: 数据库['default']['HOST'] = '127.0.0.1'

              这有助于解决此错误。

              【讨论】:

                【解决方案9】:

                转到 aws 实例 -> 安全组 -> 源 -> 入站 -> ::0

                【讨论】:

                  【解决方案10】:

                  您可能没有连接到正确的数据库。如果您使用 Docker,请确保您使用 5432 作为镜像外部的端口。

                  例如:

                  分贝: 图片:postgres:12.0-alpine 卷: - postgres_data:/var/lib/postgresql/data/ 环境文件: - ./postgres/.env 端口: - 5432:5432

                  【讨论】:

                    【解决方案11】:

                    我发现 Django/Postgres(尤其是 Docker)常见的一个问题是您的 Django Web 应用程序可能在您的 Postgres 服务器启动之前启动。如果其他解决方案不起作用,请在启动后尝试重新启动您的 Django App。使用 Docker,命令将是 docker restart &lt;web-app container name&gt;

                    【讨论】:

                    • 你只需在 docker-compose 文件上添加depends_on。
                    猜你喜欢
                    • 2020-12-09
                    • 2019-02-20
                    • 2015-12-24
                    • 2017-03-31
                    • 1970-01-01
                    • 2019-10-08
                    • 2020-01-24
                    • 1970-01-01
                    相关资源
                    最近更新 更多