“psql:无法连接到服务器:连接被拒绝"连接远程数据库时出错 [英] "psql: could not connect to server: Connection refused" Error when connecting to remote database

查看:266
本文介绍了“psql:无法连接到服务器:连接被拒绝"连接远程数据库时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

psql -h host_ip -U db_username -d db_name

这是发生的错误:

<块引用>

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

  1. 安装的 Postgres 版本是 9.4.
  2. 主机操作系统:Ubuntu 15.04
  3. 客户端操作系统:Centos 7

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

  1. 编辑了 pg_hba.conf 文件以包含

<块引用>

托管所有 0.0.0.0/0 md5

  1. 编辑'postgresql.conf'并将listen参数更改为

<块引用>

listen_addresses='*'

  1. 重新启动 postgres 服务.
  2. 在主机和客户端上禁用防火墙和 iptables.
  3. 我通过在本地运行 psql 命令进行了检查,它工作正常.
  4. 我尝试了此问题中给出的第二种解决方案.运行 nmap 给了我以下输出:

在 2015-09-07 18:08 IST 开始 Nmap 6.47 ( http://nmap.org )10.17.250.250 的 Nmap 扫描报告主机已启动(0.0000040 秒延迟).未显示:997 个关闭端口港口国务22/tcp 打开 ssh25/tcp 打开 smtp80/tcp 打开http

我是不是遗漏了什么.希望有人能帮忙.

解决方案

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

打开名为 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

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

重启你的服务器

sudo/etc/init.d/postgresql restart

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

This the error that occurs:

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

  1. Postgres installed version is 9.4.
  2. Host operating system : Ubuntu 15.04
  3. Client operating system : Centos 7

I already tried the following but the issue remains unresolved:

  1. Edited pg_hba.conf file to include

host all all 0.0.0.0/0 md5

  1. Edited 'postgresql.conf' and changed the listen parameter to

listen_addresses='*'

  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:

Starting Nmap 6.47 ( http://nmap.org ) at 2015-09-07 18:08 IST Nmap scan report for 10.17.250.250 Host is up (0.0000040s latency). Not shown: 997 closed ports PORT STATE SERVICE 22/tcp open ssh 25/tcp open smtp 80/tcp open http

Am I missing something. Hope someone can help.

解决方案

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

open file named postgresql.conf

sudo vi postgresql.conf

add this line to that file

listen_addresses = '*'

then open file named pg_hba.conf

sudo vi pg_hba.conf

and add this line to that file

host  all  all 0.0.0.0/0 md5

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

restart your server

sudo /etc/init.d/postgresql restart

这篇关于“psql:无法连接到服务器:连接被拒绝"连接远程数据库时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆