PostgreSQL之连接失败的问题及解决


    目录
  • PostgreSQL连接失败问题
    • 问题
    • 解决
    • 解释
  • PostgreSQL连接出错 could not connect to server:Connection refused(0x0000274D/10061)
    • 方法如下
    • 如果没成功那就进行以下步骤              
  • 总结

    PostgreSQL连接失败问题
    问题
    在pgAdmin中,将本地数据库连接的host由localhost或127.0.0.1改为自己的真实ip–10.xxx.xxx.xxx后,连接失败
    报错如下:
    
    psql: could not connect to server: Connection refused  
    Is the server running on host "my host name" (IP) and accepting  
    TCP/IP connections on port 5432?  
    解决
    出现上述问题时,需修改postgresql.conf和pg_hba.conf文件。
    此文件位于postgresql数据文件目录中,默认为/var/lib/pgsql/data/。
    将postgresql.conf修改如下:listen_addresses = '*'。
    pg_hba.conf中增加需要连接该数据库主机的ip地址。
    如下所示,增加对主机10.xxx.xxx.xxx的信任。
    
    host    all             all             10.xxx.xxx.xxx/32         trust
    如上设置后,使用10.xxx.xxx.xxx可正常连接。
    解释
    原来,在客户端访问PostgreSQL数据库时,PostgreSQL会读取文件pg_hba.conf判断是否信任该主机,故所有需要连接PostgreSQL Server的主机都应当在pg_hba.conf中添加对其信任,即使是Server主机也不例外!
    PostgreSQL连接出错 could not connect to server:Connection refused(0x0000274D/10061)
    使用navicat连接本地PostgreSQL数据库时报错:
    
    could not connect to server: Connection refused (0x0000274D/10061)  Is the server running on host"localhost" (:1) and acceptingTCP/IP connections on port 5433 ?
    could not connect to server: Connection refused (0x0000274D/10061)  Is the server running on host"localhost" (127.0.0.1) and acceptingTCP/IP connections on port 5433?
    
    首先检查一下是不是没有启动PostgreSQL服务,因为没启动服务可能会报这个错误(我就是);
    方法如下
    win+R打开输入命令框,输入services.msc打开服务列表。
    
    右键启动
    
    再次尝试连接数据库,看是否成功。
    如果没成功那就进行以下步骤              
     1、在postgresql的安装文件夹\9.5\data\pg_hba.conf里面找到“# IPv4 local connections:”
    
    然后在这行上面添加“local pgsql all trust”,             
    在它下面的“host    all         all         127.0.0.1/32          md5”             
    下面添加一行,内容为“host all all 192.168.91.1/24 md5”   
    
    注:127.0.0.1/32和192.168.91.1/24中的32与24,用32表示该IP被固定,用24表示前3位固定,后面一位可以由自己设,这样,前3位ip地址与该设定相同的计算机就可以访问postgresql数据库。                 
    2、PostgreSQL\9.5\data\postgresql.conf文件中,找到“#listen_addresses = 'localhost'”,把它改成“listen_addresses = '*'”。
    这样,postgresql就可以监听所有ip地址的连接。   
       
    
    3、然后重启postgresql服务。如果系统启用了防火墙,请先关闭。如果要使用pgadmin连接远程的数据库服务器,须在SSL的选项中选择允许。
    总结
    以上为个人经验,希望能给大家一个参考,也希望大家多多支持电脑手机教程网。