You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PostgreSQL运行psql命令报错,重启无效,求问题原因及解决办法

PostgreSQL连接错误:无法找到Unix套接文件的排查方案

你的错误信息如下:

postgres@lennox-ThinkPad-T470s:~$ psql
psql: error: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

重启服务无效的情况下,可从以下几个方向排查:

  • 确认服务是否真的启动成功
    执行systemctl status postgresql(systemd系统)或service postgresql status查看服务状态。如果显示未启动,尝试systemctl start postgresql手动启动;若启动失败,通过journalctl -u postgresql查看系统日志,定位启动失败的具体原因(比如数据目录权限、配置文件语法错误)。

  • 检查套接文件的实际路径
    部分PostgreSQL版本或自定义配置会将套接文件放在/tmp目录而非/var/run/postgresql。执行find / -name ".s.PGSQL.5432" 2>/dev/null查找实际路径:

    • 如果找到路径是/tmp/.s.PGSQL.5432,可通过psql -h /tmp指定套接字目录连接;
    • 也可以修改postgresql.conf中的unix_socket_directories参数,添加正确路径后重启服务。
  • 排查套接文件及目录的权限
    执行ls -ld /var/run/postgresql检查目录权限,确保postgres用户拥有读写权限,且当前连接用户(若不是postgres)属于postgres用户组。
    临时解决可尝试切换到postgres用户再连接:sudo -u postgres psql。

  • 验证配置文件正确性

    • 检查postgresql.conf:确认listen_addresses未限制本地连接(默认值即可),port设置为5432;
    • 检查pg_hba.conf:确保存在允许本地连接的规则,比如local all all peer或local all all trust;
      修改配置后需重启服务生效。
  • 检查数据目录是否正常初始化
    若为新安装的PostgreSQL,可能数据目录未正确初始化。执行sudo -u postgres initdb -D /var/lib/postgresql/[你的版本号]/main重新初始化(替换为实际数据目录路径),注意操作前备份已有数据。

内容的提问来源于stack exchange,提问作者knoxx04

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 10:25:03