简体   繁体   English

错误:抱歉,已经有太多客户了

[英]Error: sorry, too many clients already

I am using PostgreSQL in my NodeJS backend service.我在我的 NodeJS 后端服务中使用 PostgreSQL。 All of sudden, when I start the service I am facing below error突然之间,当我启动服务时,我面临以下错误

 connection error error: sorry, too many clients already.

PostgresSQL connection config PostgresSQL 连接配置

 const pg = require(“pg”);
 const client = new pg.Client({
        host: “txslmxxxda6z”,
        user: “mom”,
        password: “mom”,
        db: “mom”,
        port: 5025
 });

I am unable to query database because of the above error.由于上述错误,我无法查询数据库。 I am unable to fix this issue.我无法解决这个问题。 Can you please suggest the solution你能建议解决方案吗

below query will help you.下面的查询会帮助你。

select max_conn,used,res_for_super,max_conn-used-res_for_super res_for_normal 
from 
  (select count(*) used from pg_stat_activity) t1,
  (select setting::int res_for_super from pg_settings where name=$$superuser_reserved_connections$$) t2,
  (select setting::int max_conn from pg_settings where name=$$max_connections$$) t3

I experienced the same issue that " sorry, too many clients already. Request timeout".我遇到了同样的问题,“对不起,已经有太多的客户了。请求超时”。 I managed to solve it.我设法解决了它。 There are some ways which you can try有一些方法你可以试试

1)The first one that you can increase the number of maximum connections and other settings in sequilize like 1)第一个可以在sequilize中增加最大连接数和其他设置,例如

 { max: 95, //maximum connection which postgresql or mysql can intiate min: 0, //maximum connection which postgresql or mysql can intiate acquire:20000, // time require to reconnect idle: 20000, // get idle connection evict:10000 // it actualy removes the idle connection }

2). 2)。 The second way is to use timescaledb because sometimes with complex queries we get timeout error for timescale data you can set timescaledb so you can run complex database queries.第二种方法是使用 timescaledb,因为有时对于复杂查询,我们会收到 timescale 数据的超时错误,您可以设置 timescaledb,以便运行复杂的数据库查询。 you can also set maximum clients for postgres according to your server requirement.您还可以根据您的服务器要求为 postgres 设置最大客户端。

https://github.com/timescale/timescaledb-tune https://github.com/timescale/timescaledb-tune

If you would like to use PgAdmin (for me it is more than convenient), you could do these simple steps.如果您想使用 PgAdmin(对我来说这很方便),您可以执行这些简单的步骤。 And more than that, please use dashboard of this tool that will show you all you connections.不仅如此,请使用此工具的仪表板,它将向您显示所有连接。

in the documentation https://node-postgres.com/api/pool we can see the "max" and " idleTimeoutMillis" variables, so play with your config, and start from this:在文档https://node-postgres.com/api/pool中,我们可以看到“max”和“idleTimeoutMillis”变量,因此请使用您的配置,并从以下内容开始:

const poolCredentials = {
    user: process.env.DB_USER,
    host: process.env.DB_HOST,
    database: process.env.DB_NAME,
    password: process.env.DB_PW,
    port: process.env.DB_PORT,
    idleTimeoutMillis: 1,
    max: 10,
    connectionTimeoutMillis: 2000,
    ...ssl
};

You simply look on you server constraints and change the config.您只需查看服务器约束并更改配置。 Good luck and glad if this is helps祝你好运,很高兴如果这有帮助

SELECT * FROM pg_stat_activity;

例子

Have you an idea how your program managed to open many connections to the DB ?您知道您的程序如何设法打开与数据库的许多连接吗? Which is your connection-management strategy:您的连接管理策略是什么:

  1. Open a short-lived connection for each query/command/batch/call, or为每个查询/命令/批处理/调用打开一个短期连接,或
  2. Open a single (or few) connection(s) with a the lifespan of the entire service?在整个服务的生命周期内打开一个(或几个)连接?

Are you sure you are closing every connection that you open?您确定要关闭您打开的每个连接吗? To avoid concurrency issues, I use the first strategy in my NodeJS applications, opening a dedicated connection to handle each request, and make absolutely sure to close it when the request has been handled.为了避免并发问题,我在我的NodeJS应用程序中使用第一个策略,打开一个专用连接来处理每个请求,并确保在处理请求后关闭它。 That way, the maximum number of open connections is the maximum number of simultaneously processed requests.这样,打开连接的最大数量就是同时处理的请求的最大数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM