# Too many connections to DB, how to optimize

**URL:** <https://meta.discourse.org/t/too-many-connections-to-db-how-to-optimize/66395>\
**Category:** Support\
**Created:** [2017 年7 月 18 日 15:40 UTC](https://meta.discourse.org/t/too-many-connections-to-db-how-to-optimize/66395 "2017-07-18T15:40:01Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

作者： ![TomekB](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tomekb/32/66593_2.png) [@TomekB](https://meta.discourse.org/u/TomekB)\
发布日期： [2017 年7 月 18 日 15:40 UTC](https://meta.discourse.org/t/too-many-connections-to-db-how-to-optimize/66395/1 "2017-07-18T15:40:02Z")

</div>

I have frequent 500 errors due to exceeding number of connections to my DB.

This results in errors like this in /logs:

```
Job exception: FATAL: remaining connection slots are reserved for non-replication superuser connections

```

My config:

```
UNICORN_WORKERS: 12
UNICORN_SIDEKIQS: 2
DISCOURSE_DB_POOL: 90

```

My DB under load performs like this:

 ![](https://global.discourse-cdn.com/meta/original/3X/a/5/a59c95bfdbacc1f5037e2b7c304f67ff7c0170df.png)

How can I optimize this? Please keep in mind the default config of PSQL has max connections limit set to 100.

I have plenty of RAM and CPUs for both web\_data container and data container (which is hosted on a second server).

Update: I have a lot of open connections to DB from sidekiq, a lot more than it should (I have just 2 sidekiqs with 5 threads each).

---

_[View the full topic](https://meta.discourse.org/t/too-many-connections-to-db-how-to-optimize/66395)._
