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

时间:2015-04-29 07:44:59

标签: 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?

当我尝试连接到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',
    }
}

9 个答案:

答案 0 :(得分:37)

可能是一些问题:

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

  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

  6. ,它应该正常运行

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

答案 1 :(得分:2)

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

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

答案 2 :(得分:2)

适用于Windows

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

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

答案 3 :(得分:1)

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

答案 4 :(得分:1)

重新启动服务器解决了我这方面的问题。

答案 5 :(得分:1)

如果您在使用Google Cloud中的PostgresSQL时看到此错误,请按照 https://cloud.google.com/python/django/flexible-environment#macos-64-bit

还按如下所示分隔主机配置

DATABASES ['default'] ['HOST'] ='/ cloudsql /' 如果os.getenv('GAE_INSTANCE'): 通过 其他: DATABASES ['default'] ['HOST'] ='127.0.0.1'

这有助于解决此错误。

答案 6 :(得分:0)

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

答案 7 :(得分:0)

以下命令对我有效(Windows)-

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

然后再次运行服务器-

python manage.py runserver

答案 8 :(得分:0)

在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