PostgreSQL FATAL: sorry, too many clients already 连接数爆满的处理办法
连接PSQL报错信息如下,无法连接
[root@dbeta data]# sudo -u postgres psql
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: sorry, too many clients already
程序的报错
FATAL: sorry, too many clients already
问题原因在于当前连接数超过了postgres.conf中配置的max_connections
举个例子:max_connections = 100
当你的连接数等于100时,你就无法再创建新的连接,就会报错,跟上面一样
继续向下排查,
理论上,使用postgres用户应该是可以连接的,因为superuser_reserved_connections参数会给superuser预留3个连接,但是我们还是无法连接,此时查看进程信息
[root@beta data]# ps aux | grep postgres
postgres 2180 0.0 0.5 308388 21404 ? Ss 2024 15:55 /usr/bin/postmaster -D /var/lib/pgsql/data
postgres 2182 0.0 0.1 162824 4840 ? Ss 2024 0:00 postgres: logger
postgres 2185 0.0 3.7 308760 145728 ? Ss 2024 25:51 postgres: checkpointer
postgres 2186 0.0 3.6 308528 143624 ? Ss 2024 47:59 postgres: background writer
postgres 2187 0.0 0.2 308388 10112 ? Ss 2024 34:21 postgres: walwriter
postgres 2188 0.0 1.7 308928 70296 ? Ss 2024 4:22 postgres: autovacuum launcher
postgres 2189 0.0 0.1 163112 4932 ? Ss 2024 58:17 postgres: stats collector
postgres 2190 0.0 0.1 308812 6624 ? Ss 2024 0:08 postgres: logical replication launcher
postgres 23124 1.0 3.8 311108 150332 ? Ss 23:03 0:11 postgres: postgres postgres 192.168.1.108(39140) idle
postgres 23213 1.1 3.7 310292 149184 ? Rs 23:04 0:11 postgres: postgres postgres 192.168.1.108(46384) SELECT
postgres 23214 1.1 3.8 310320 149460 ? Ss 23:04 0:10 postgres: postgres postgres 192.168.1.108(46400) idle
postgres 23361 1.3 3.7 310348 148952 ? Ss 23:06 0:11 postgres: postgres postgres 192.168.1.108(56810) idle
postgres 23362 1.2 3.7 310268 148840 ? Ss 23:06 0:11 postgres: postgres postgres 192.168.1.108(56812) idle
postgres 23363 1.2 3.7 310348 149200 ? Ss 23:06 0:11 postgres: postgres postgres 192.168.1.108(56828) idle
postgres 23364 1.2 3.7 310348 149088 ? Ss 23:06 0:11 postgres: postgres postgres 192.168.1.108(56832) idle
看这里
postgres: postgres postgres 192.168.1.108(56832) idle
我们居然发现,程序使用的是postgres用户,连接的postgres库
是使用的superuser用户去连接的
这就导致我们使用postgres用户也无法连接上去,即使pg给我们预留了三个位置
这种情况就只能kill连接的进程,或者重启pg
总结下
1.程序连接pg时,使用项目用户,不要使用postgres用户,否则像这种连接数满了的情况,都无法去查看信息
2.学习superuser_reserved_connections参数的用法