# 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`
- **Domain:** python
- **Category:** network_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

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

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.4.x | active | — | — |
| 2.0.x | active | — | — |

## Workarounds

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

## Dead Ends

- **** — 尝试连接其他端口，但不知道服务器实际监听端口。 (50% fail)
- **** — 禁用防火墙，但可能造成安全隐患。 (40% fail)
