python network_error ai_generated true

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused

ID: python/postgresql-unable-to-connect

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2025-09-25First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.4.x active
2.0.x active

Root Cause

PostgreSQL 服务器未启动或监听在不同端口/地址。

generic

中文

PostgreSQL 服务器未启动或监听在不同端口/地址。

Workarounds

  1. 90% success
    启动 PostgreSQL 服务:systemctl start postgresql 或 service postgresql start
  2. 85% success
    检查连接配置,确保主机、端口和数据库名称正确。

Dead Ends

Common approaches that don't work:

  1. 50% fail

    尝试连接其他端口,但不知道服务器实际监听端口。

  2. 40% fail

    禁用防火墙,但可能造成安全隐患。