关于python:django.db.utils.OperationalError无法连接到服务器

django.db.utils.OperationalError Could not connect to server

我不知道怎么解决这个问题

我不知道当我尝试runserver时为什么会出现这种错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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时:

1
2
3
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"?

设置:

1
2
3
4
5
6
7
8
9
10
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'beerad',
        'USER': 'bli1',
        'PASSWORD': '',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}


可能有一些问题:

  • PostgreSQL没有运行。与sudo service postgresql status核对
  • Postgressql没有在端口5432上运行。你可以通过输入sudo netstat -nl | grep postgres来检查它。

  • 尝试连接数据库时出错,如用户名、密码或数据库名。检查它们是否是Postgres要求您连接的内容,以及您要访问的数据库名称。

  • Postgres中postmaster.pid的问题。这种情况可能是由于一次不正常的关机而发生的。它提醒pid活动,这不允许服务器启动。要修复它,您必须:

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

    在此之后,如果您使Postgres的runserver

  • MacOSX的帮助:如何在MacOSX上启动PostgreSQL服务器?


    对于Windows

    进入搜索栏,只需写"Open psql",然后点击回车。

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


    尝试杀死所有Postgres进程。我在Mac上,我在Ubuntus论坛上找到的这个解决方案真的很有效。

    https://askubuntu.com/questions/547434/how-to-finally-stop-all-postgres-processesenter image description here


    在我的例子中,所有的设置都很好,Postgres有正确的端口,PostgreSQL运行正常,但是5432端口是与phppgadmin共享的,我可以访问phppgadmin,这个phppgadmin使我可以访问Postgres数据库服务器,但是我的django应用程序不工作,它会返回拒绝连接的错误。所以我把phppgadmin配置文件(/etc/phppgadmin/config.inc.php)上的端口号从5432改为5433,一切都很好。