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

postgresql.service已运行但无法执行psql及连接pgAdmin的问题

问题描述

相关报错截图如下:

  • psql命令执行报错截图
  • pgAdmin注册服务器报错截图

执行psql命令时出现以下错误:

psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?

在pgAdmin中尝试注册新服务器时出现连接拒绝错误:

Unable to connect to server: connection failed: :1), port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections? connection to server at "localhost (127.0.0.1), port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?

已执行sudo systemctl status postgresql和sudo systemctl status 'postgresql*',结果显示PostgreSQL服务处于激活状态,但无法正常使用psql命令及连接pgAdmin。

解决步骤

1. 检查PostgreSQL实际运行参数

执行命令查看PostgreSQL进程的真实配置:

ps aux | grep postgres

重点关注输出中的-p(端口)和-k(套接字目录)参数:

  • 如果端口不是默认的5432,连接时需指定端口:psql -p <实际端口号>
  • 如果套接字路径不是默认的/var/run/postgresql/,设置环境变量后再连接:
    export PGHOST=<实际套接字路径>
    psql
    

2. 修正PostgreSQL核心配置

(1)调整postgresql.conf

编辑对应版本的配置文件(将<版本号>替换为实际安装的PostgreSQL版本,比如14):

sudo nano /etc/postgresql/<版本号>/main/postgresql.conf

确保以下配置项正确:

  • listen_addresses = 'localhost'(如需允许远程连接可设为*)
  • port = 5432(若自定义端口,需与进程实际运行参数一致)

修改完成后重启服务:

sudo systemctl restart postgresql

(2)更新pg_hba.conf认证规则

编辑认证配置文件:

sudo nano /etc/postgresql/<版本号>/main/pg_hba.conf

确保本地连接规则存在且正确:

local   all             all                                     peer
host    all             all             127.0.0.1/32            scram-sha-256
host    all             all             ::1/128                 scram-sha-256

保存后重启PostgreSQL服务。

3. 修复套接字目录权限或路径

如果默认套接字目录不存在或权限异常,执行以下命令修复:

sudo mkdir -p /var/run/postgresql
sudo chown postgres:postgres /var/run/postgresql
sudo chmod 775 /var/run/postgresql

修复完成后重启服务。

4. 查看服务日志排查深层问题

通过日志查看是否存在实例崩溃、配置语法错误、磁盘空间不足等问题:

sudo journalctl -u postgresql -f

根据日志中的具体错误提示进行针对性修复。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 00:53:27