# psycopg2.OperationalError: 无法连接到服务器：没有那个文件或目录
	服务器是否在本地运行并通过 Unix 域套接字 "/var/run/postgresql/.s.PGSQL.5432" 接受连接？

- **ID:** `database/postgresql-cannot-connect-to-server-no-such-file-or-directory`
- **领域:** database
- **类别:** connection_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

PostgreSQL Unix 域套接字文件在预期路径上不存在，通常是因为服务器未运行、套接字目录配置错误或客户端在错误的位置查找。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| PostgreSQL 15.4 | active | — | — |
| PostgreSQL 16.1 | active | — | — |
| PostgreSQL 14.9 | active | — | — |

## 解决方案

1. ```
   Start the PostgreSQL server: `sudo systemctl start postgresql` or `pg_ctl start -D /var/lib/postgresql/15/main` (adjust version and data directory).
   ```
2. ```
   If the server is running but socket is in a different directory, set the host parameter to use TCP/IP: `host=localhost port=5432` in the connection string, or set `PGHOST=localhost` environment variable.
   ```

## 无效尝试

- **** — If the server is not running, TCP/IP connection also fails with 'Connection refused'. This only masks the real issue. (70% 失败率)
- **** — The issue is server-side (socket file missing), not client-side. Reinstalling clients doesn't fix the server. (95% 失败率)
