python network_error ai_generated true

django.db.utils.OperationalError: connection refused - check hostname and port

ID: python/django-database-connection-refused

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-06-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

Database server is not running or connection parameters are incorrect.

generic

中文

数据库服务器未运行或连接参数不正确。

Workarounds

  1. 80% success
    Start database service: sudo systemctl start postgresql (or mysql)
  2. 90% success
    Verify settings: DATABASES = {'default': {'ENGINE': 'django.db.backends.postgresql', 'NAME': 'mydb', 'USER': 'user', 'PASSWORD': 'pass', 'HOST': '127.0.0.1', 'PORT': '5432'}}

Dead Ends

Common approaches that don't work:

  1. 50% fail

    Changing database engine in settings without restarting server won't take effect.

  2. 30% fail

    Using localhost instead of 127.0.0.1 can cause IPv6 resolution issues.