postgres链接太多(sorry, too many clients already)

postgres链接太多(sorry, too many clients already)-CSDN博客

postgres 由于用户链接数已满无法进行新的链接,同时提示错误 sorry, too many clients already

首先进入postgres数据库,我使用的容器部署,那就是先要进入对应的postgres 容器

$ docker exec -u root -it postgresId /bin/bash

1、登录postgres

$ su postgres

2、进入sql 查询

$ psql

2、当前总共正在使用的连接数

postgres=# select count(1) from pg_stat_activity;
 count 
-------
    23
(1 row)

3、显示系统允许的最大连接数

$ show max_connections;

4、显示系统保留的用户数

$ show superuser_reserved_connections ;

5、按照用户分组查看

$ select usename, count(*) from pg_stat_activity group by usename order by count(*) desc;

6、查询当前所有连接的状态

postgres=# select datname,pid,application_name,state from pg_stat_activity; 
 datname  | pid  |    application_name    | state  
----------+------+------------------------+--------
 postgres |   47 | psql                   | idle
 app      | 5884 | PostgreSQL JDBC Driver | idle
 app      | 5888 | PostgreSQL JDBC Driver | idle
 app      | 5875 | PostgreSQL JDBC Driver | idle

7、关闭当前state为 idle 空闲状态的连接

postgres=# select pg_terminate_backend(5884) from pg_stat_activity;
 pg_terminate_backend 

8、修改最大链接数据

如果不想关闭空闲链接,可以增加最大链接数

postgres=# show max_connections;
postgres=# alter system set max_connections=1000;

重启数据库再查看链接就已经是1000了

 

posted @ 2025-05-12 17:47  CharyGao  阅读(166)  评论(0)    收藏  举报