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

pgAdmin4无法注册服务器及psql连接报错问题求助

PostgreSQL 连接问题解决指南

问题1:psql 报错“database 'james' does not exist”

原因

PostgreSQL 创建用户(角色)时,不会自动生成与用户名同名的数据库。默认仅超级用户 postgres 会自带同名初始数据库,其他用户需手动创建对应数据库。此外,执行 psql 时若不指定数据库,它会尝试连接与当前系统用户名同名的数据库,你用 james 用户执行 psql 时,自然会寻找不存在的 james 数据库。

解决步骤

  • 切换到系统默认PostgreSQL超级用户 postgres:
    sudo su - postgres
    
  • 创建归属 james 用户的同名数据库:
    createdb -O james james
    
    (-O james 指定该数据库的所有者为 james)
  • 此时用 james 用户执行 psql 即可直接连接;也可临时指定连接默认的 postgres 数据库:
    psql -d postgres -U james
    

问题2:pgAdmin4 注册服务器报错“Connection refused”

原因分析

你的 postgresql.service 状态为 active (exited),这是系统级占位服务,实际运行的PostgreSQL实例服务通常是 postgresql@<版本号>-main.service(如 postgresql@14-main.service)。另外,psql报错提到的socket端口是5433,但你pgAdmin用的是5432,说明实例实际监听端口并非默认值,同时可能未开启TCP/IP连接权限。

解决步骤

  • 检查实际运行的PostgreSQL实例服务:
    systemctl list-units --type=service | grep postgresql
    
    找到类似 postgresql@14-main.service 的活跃服务,若未运行则启动:
    sudo systemctl start postgresql@14-main.service
    
  • 确认实例监听端口:
    查看对应版本的配置文件(路径示例:/etc/postgresql/14/main/postgresql.conf):
    sudo grep "port = " /etc/postgresql/14/main/postgresql.conf
    
    若端口为5433,需在pgAdmin中将连接端口改为5433;若要改为默认5432,修改后重启服务:
    sudo systemctl restart postgresql@14-main.service
    
  • 开启本地TCP/IP连接权限:
    编辑 pg_hba.conf(同上述配置目录),添加或确保存在以下规则:
    host    all             all             127.0.0.1/32            scram-sha-256
    host    all             all             ::1/128                 scram-sha-256
    
    同时修改 postgresql.conf 中的监听地址:
    listen_addresses = 'localhost'
    
    修改后重启服务生效:
    sudo systemctl restart postgresql@14-main.service
    
  • 重新配置pgAdmin4服务器连接:
    将端口改为实例实际监听的数值(如5433),保持Host为localhost、Username为james,再尝试连接。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 14:23:09