python network_error ai_generated true

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

ID: python/sqlalchemy-connection-refused

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2025-06-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

The database server is not running or not accepting connections on the specified host and port.

generic

中文

数据库服务器未运行或未在指定的主机和端口上接受连接。

Workarounds

  1. 95% success Start the database server
    # For PostgreSQL:
    sudo systemctl start postgresql
    # Or:
    pg_ctl start
  2. 90% success Check if the server is listening on the correct interface
    sudo netstat -tulpn | grep 5432
    # If not, edit postgresql.conf to listen on '*' or '0.0.0.0'

Dead Ends

Common approaches that don't work:

  1. Changing the port number arbitrarily 80% fail

    The database is not listening on that port either.

  2. Disabling firewall completely 60% fail

    This is a security risk and may not solve the issue if the server is not running.