首页 > 数据库 > 数据库开发技术 > PostgreSQL常见连接报错处理
作者:Digital Observer(施嘉伟)
Oracle ACE Associate: Database
PostgreSQL ACE Partner
11年数据库行业经验,现主要从事数据库服务工作
拥有Oracle OCM、DB2 10.1 Fundamentals、MySQL 8.0 OCP、WebLogic 12c OCA、KCP、PCTP、PCSD、OCI、PolarDB技术专家、达梦师资认证、数据安全咨询高级等认证
ITPUB认证专家、PolarDB开源社区技术顾问
国内某高校企业实践指导教师
公众号:Digital Observer;CSDN:施嘉伟;ITPUB:sjw1933;PGFans:施嘉伟。
PostgreSQL连接报错可能由多种原因引起,包括但不限于:服务器未运行、配置文件(如`postgresql.conf`和`pg_hba.conf`)设置不当、网络问题、防火墙设置阻止连接、身份验证失败、连接信息(如主机名、端口号、用户名和密码)有误或超时设置不当等。今天我们分享比较常规的一些问题。
PostgreSQL在初始化以及其他的配置变动会引发的一些连接失败问题,本文档收录了一些常见的场景及处理办法。
$ psql -h 10.0.4.13 -U postgres -d postgres -p5432
psql: error: could not connect to server: Connection refused
Is the server running on host "10.0.4.13" and accepting
TCP/IP connections on port 5432?
本地可连:
$ psql -p 5432
psql (13.4)
Type "help" for help.
postgres=#通过本地连接PostgreSQL,查看当前listen_addresses参数设置
$ psql -p 5432
psql (13.4)
Type "help" for help.
postgres=# show listen_addresses ;
listen_addresses
------------------
localhost
(1 row)在PostgreSQL数据库目录下的postgresql.conf文件更改此项参数,并且重启数据库生效
vi $PGDATA/postgresql.conf
listen_addresses = '*'
查看PostgreSQL数据目录下pg_hba.conf的内容,是否允许指定IP或网段远程访问数据库。
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
以上只允许所有用户拥有本地连接的访问权限,需要修改并添加如下规则,根据业务需要配置,修改或添加完成后重新加载配置即可生效
$ pg_ctl reload -D $PGDATA
server signaled
允许所有网段的所有用户免密的方式访问数据库:
host all all 0.0.0.0/0 trust
允许所有网段的所有用户通过md5验证的方式访问数据库:
host all all 0.0.0.0/0 md5允许所有网段的user1用户通过md5验证的方式访问数据库:
host all user1 0.0.0.0/0 md5
允许192.168.0.0/16网段的所有用户通过md5验证的方式访问数据库:
host all all 192.168.0.0/16 md5允许192.168.0.0/16网段的所有用户通过md5验证的方式访问db1数据库:
host db1 all 192.168.0.0/16 md5远程、本地都无法连接
远程:
$ psql -h 10.0.4.13 -U postgres -d postgres -p5432
psql: error: could not connect to server: Connection refused
Is the server running on host "10.0.4.13" and accepting
TCP/IP connections on port 5432?
本地:
$ psql -p 5432
psql: error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
检查主机IP以及端口号等连接信息
查看端口号,以下为正确的情况,如果没有,则使用正确的端口号连接
$ netstat -tunlp | grep 5432
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 11186/postgres
tcp6 0 0 :::5432 :::* LISTEN 11186/postgres或者查看服务有没有启动
未启动:
$ pg_ctl status -D $PGDATA
pg_ctl: no server running
启动:
$ pg_ctl status -D $PGDATA
pg_ctl: server is running (PID: 11186)
/software/pgsql13/bin/postgre
以上便是处理postgresql连接问题的一些基本思路,希望对你的运维工作有所帮助,也欢迎大家加我微信交流,相互学习!

我读的书愈多,就愈亲近世界,愈明了生活的意义,愈觉得生活的重要。


点击"阅读原文"
原文作者:Digital Observer;如有侵权,请联系管理员删除。