django.db.utils.OperationalError无法连接到服务器

Lio*_*cer 22 python django postgresql

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

我不知道为什么我在尝试时遇到此错误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?
Run Code Online (Sandbox Code Playgroud)

当我尝试连接到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"?
Run Code Online (Sandbox Code Playgroud)

settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'beerad',
        'USER': 'bli1',
        'PASSWORD': '',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}
Run Code Online (Sandbox Code Playgroud)

Ric*_*llo 39

这可能是一些问题:

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

  3. 尝试连接到数据库时遇到错误,例如用户名,密码或数据库名.检查它们是什么postgres要求您连接它,这是您要访问的db_name.

  4. postgres中postmaster.pid的问题.由于未正确完成关闭,可能会发生这种情况.它提醒一个不允许你的服务器启动的pid活着.要解决它,你必须:

     * rm /usr/local/var/postgres/postmaster.pid 
     * pg_resetxlog -f /usr/local/var/postgres
    
    Run Code Online (Sandbox Code Playgroud)

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

Mac OSX中的帮助:如何在Mac OS X上启动PostgreSQL服务器?

  • 我试用了您提供的2个命令:`bash:service:找不到命令`和`netstat:lunt:未知或未使用的协议` (2认同)

Abd*_*eed 7

对于 Windows

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

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


Jcc*_*ria 5

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

https://askubuntu.com/questions/547434/how-to-nicely-stop-all-postgres-processes 在此处输入图片说明


Ald*_*are 5

就我而言,一切都设置良好,Postgres 有正确的端口,PostgreSQL 正常运行,但 5432 端口与 phppgadmin 共享,我可以访问 phppgadmin,它使我可以通过 Web 访问 Postgres 数据库服务器,但我的 Django 应用程序不起作用,它将返回连接拒绝错误。所以我将 phppgadmin 配置文件(/etc/phppgadmin/config.inc.php)上的端口号从 5432 更改为 5433,一切正常。