“ psql:无法连接到服务器:连接被拒绝”连接到远程数据库时出错

“ psql:无法连接到服务器:连接被拒绝”连接到远程数据库时出错

问题描述:

我正在尝试使用以下命令连接到安装在远程服务器中的postgres数据库:

I am trying to connect to a postgres database installed in a remote server using the following command:

psql -h host_ip -U db_username -d db_name

psql -h host_ip -U db_username -d db_name

发生的错误:


psql:无法连接到服务器:连接被拒绝
服务器在主机上运行吗?并接受端口5432上的
TCP / IP连接?

psql: could not connect to server: Connection refused Is the server running on host "" and accepting TCP/IP connections on port 5432?




  1. Postgres安装的版本是9.4

  2. 主机操作系统:Ubuntu 15.04

  3. 客户端操作系统:Centos 7

我已经尝试了以下操作,但问题仍未解决:

I already tried the following but the issue remains unresolved:


  1. 已编辑 pg_hba。 conf 文件以包含

  1. Edited pg_hba.conf file to include




全部托管0.0.0.0/0 md5

host all all 0.0.0.0/0 md5




  1. 编辑'postgresql.conf'并更改了监听参数米到




listen_addresses ='*'

listen_addresses='*'




  1. 重新启动postgres服务。

  2. 在主机和客户端上禁用防火墙和iptables。

  3. 我通过在本地运行psql命令进行了检查,它是否有效。

  4. 我尝试了问题。运行 nmap 给了我以下输出:

  1. Restarted postgres service.
  2. Disabled firewall and iptables on host and client.
  3. I checked by running the psql command locally and it worked.
  4. I tried the second solution given in this question. Running nmap gave me the following output:

从2015年9月7日18:08开始Nmap 6.47(http://nmap.org)IST
Nmap扫描报告为10.17.250.250
主机已启动(0.0000040s延迟)。
未显示:997个关闭的端口
端口状态服务
22 / tcp开放ssh
25 / tcp开放smtp
80 / tcp开放http

我错过了什么吗?希望有人能提供帮助。

Am I missing something. Hope someone can help.

cd /etc/postgresql/9.x/main/

打开名为 postgresql.conf

open file named postgresql.conf

sudo vi postgresql.conf

将此行添加到该文件中

listen_addresses = '*'

然后打开名为 pg_hba.conf

sudo vi pg_hba.conf

将此行添加到该文件中

host  all  all 0.0.0.0/0 md5

它允许所有使用加密密码的用户访问所有数据库

It allows access to all databases for all users with an encrypted password

sudo /etc/init.d/postgresql restart