postgres登录失败Connection refused与SSL off失败

时间:2024-04-15 18:01:46

连接失败问题

使用postgres数据库连接工具测试,遇到两次失败

第一个登录失败问题

Connection to 192.168.XX.XX:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
  Connection refused: connect
  Connection refused: connect

这语句翻译下,用户名密码是正确的,但是tcp/ip的连接失败了。这个解决方法其实就是去建立tcp/ip连接过程。
原因是宿主机并没有监听其他的主机接口,因此没有建立连接。
解决方法

修改/var/lib/pgsql/9.6/data/postgresql.conf  
新增listen_addresses = \'*\'  #what IP address(es) to listen on
#默认
listen_addresses = \'localhost\'

第二个登录失败问题

The server doesn\'t grant access to the database: the server reports 
FATAL: no pg_hba.conf entry for host "192.168.X.X", user "postgres", database "postgres"
#FATA:no pg_hba.conf entry for host "192.168.113.1",user "postgres",database "postgres",SSL off

这个问题也说这个主机没有权限访问数据库,也告诉配置文件在哪:pg_hba.conf。
pg_hba.conf为PostgreSQL的访问策略配置文件,默认位于/var/lib/pgsql/9.6/data/pg_hba.conf目录(postgres 9.6)。
该配置文件有5个参数,分别为:TYPE(主机类型)、DATABASE(数据库名)、USER(用户名)、ADDRESS(IP地址和掩码)、METHOD(加密方法)。
关于这个配置文件可以参考PostgreSQL的pg_hba.conf文件讲解
解决方式增加选项支持所所有的主机ip访问
增加

host    all             all             0.0.0.0/0               md5