ITPub博客

首页 > IT职业 > IT图书 > postgresql连接失败如何处理

postgresql连接失败如何处理

IT图书 作者:m53469 时间:2021-09-11 10:24:06 0 删除 编辑

在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主机也不例外!

推荐学习《Python教程》。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2819/viewspace-2834930/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-19

  • 博文量
    80
  • 访问量
    13443