REST API don't release PostgreSQL client #4166

Closed
opened 2025-12-29 18:33:35 +01:00 by adam · 3 comments
Owner

Originally created by @liaocyintl-deprecated on GitHub (Oct 6, 2020).

Hi team,
There is a 'too many clients already' issue in our Netbox instance.

Environment

  • Python version: 3.7.4
  • NetBox version: v2.8.9

Steps to Reproduce

  1. Access any API using curl or python script repeatedly.
    Such as, curl -H "Authorization: Token [your token here]" -H "Accept: application/json;" http://netbox.url/api/dcim/sites/
  2. In the Postgres database, count the number of current database clients using 'select COUNT(*) from pg_stat_activity;'.
  3. You will find the database clients, with a state 'idle', don't be released even API access done.

Expected Behavior

The DB client should be closed when API access is done.

Observed Behavior

The database connections do not be released until a 'too many clients already' error occurred as follows.
image

Maybe there are some issues in our configuration files.
We are very glad if you provide any information for us.

Thank you very much!

Originally created by @liaocyintl-deprecated on GitHub (Oct 6, 2020). Hi team, There is a 'too many clients already' issue in our Netbox instance. ### Environment * Python version: 3.7.4 * NetBox version: v2.8.9 ### Steps to Reproduce 1. Access any API using curl or python script repeatedly. Such as, curl -H "Authorization: Token [your token here]" -H "Accept: application/json;" http://netbox.url/api/dcim/sites/ 2. In the Postgres database, count the number of current database clients using 'select COUNT(*) from pg_stat_activity;'. 3. You will find the database clients, with a state 'idle', don't be released even API access done. <!-- What did you expect to happen? --> ### Expected Behavior The DB client should be closed when API access is done. <!-- What happened instead? --> ### Observed Behavior The database connections do not be released until a 'too many clients already' error occurred as follows. ![image](https://user-images.githubusercontent.com/10112615/95165409-e291a380-07e6-11eb-9e76-320d3d2bdbcf.png) Maybe there are some issues in our configuration files. We are very glad if you provide any information for us. Thank you very much!
adam closed this issue 2025-12-29 18:33:35 +01:00
Author
Owner

@candlerb commented on GitHub (Oct 6, 2020):

Maybe there are some issues in our configuration files.

It looks like you've installed Netbox wrongly. Netbox should always be run in a virtual environment, which means the instance of psycopg2 being used should be under /opt/netbox/venv/lib not a globally-installed one. Check that the version of gunicorn which you are starting is /opt/netbox/venv/bin/gunicorn not /usr/local/bin/gunicorn.

Secondly, what does the DATABASE section in configuration.py look like? Do you have CONN_MAX_AGE set? What happens if you set it to zero? See https://docs.djangoproject.com/en/3.1/ref/databases/#persistent-connections

I can't reproduce your problem here with Netbox 2.9.4.

netbox=# select COUNT(*) from pg_stat_activity;
 count
-------
     9
(1 row)
curl -H "Accept: application/json" http://netbox/api/dcim/sites/
{"count":1,"next":null,"previous":null,"results":[{"id":1,"url":"http://netbox/api/dcim/sites/1/",...
netbox=# select COUNT(*) from pg_stat_activity;
 count
-------
     9
(1 row)

My DATABASE setting looks like this:

DATABASE = {
    'NAME': 'netbox',         # Database name
    'USER': 'netbox',               # PostgreSQL username
    'PASSWORD': 'XXXXXXXX',         # PostgreSQL password
    'HOST': 'localhost',      # Database server
    'PORT': '',               # Database port (leave blank for default)
    'CONN_MAX_AGE': 300,      # Max database connection age
}
@candlerb commented on GitHub (Oct 6, 2020): > Maybe there are some issues in our configuration files. It looks like you've installed Netbox wrongly. Netbox should always be run in a virtual environment, which means the instance of psycopg2 being used should be under `/opt/netbox/venv/lib` not a globally-installed one. Check that the version of gunicorn which you are starting is `/opt/netbox/venv/bin/gunicorn` not `/usr/local/bin/gunicorn`. Secondly, what does the `DATABASE` section in configuration.py look like? Do you have `CONN_MAX_AGE` set? What happens if you set it to zero? See https://docs.djangoproject.com/en/3.1/ref/databases/#persistent-connections I can't reproduce your problem here with Netbox 2.9.4. ``` netbox=# select COUNT(*) from pg_stat_activity; count ------- 9 (1 row) ``` ``` curl -H "Accept: application/json" http://netbox/api/dcim/sites/ {"count":1,"next":null,"previous":null,"results":[{"id":1,"url":"http://netbox/api/dcim/sites/1/",... ``` ``` netbox=# select COUNT(*) from pg_stat_activity; count ------- 9 (1 row) ``` My DATABASE setting looks like this: ``` DATABASE = { 'NAME': 'netbox', # Database name 'USER': 'netbox', # PostgreSQL username 'PASSWORD': 'XXXXXXXX', # PostgreSQL password 'HOST': 'localhost', # Database server 'PORT': '', # Database port (leave blank for default) 'CONN_MAX_AGE': 300, # Max database connection age } ```
Author
Owner

@jeremystretch commented on GitHub (Oct 6, 2020):

As @candlerb points out, this is almost certainly an installation/configuration issue, and not a bug. (Please also note that a bug report would be accepted only for a recent v2.9 release as v2.8 is no longer supported.) I suggest re-installing NetBox per the official documentation to avoid such issues. If you continue to encounter problems after reinstallation, please ask for assistance on our mailing list.

@jeremystretch commented on GitHub (Oct 6, 2020): As @candlerb points out, this is almost certainly an installation/configuration issue, and not a bug. (Please also note that a bug report would be accepted only for a recent v2.9 release as v2.8 is no longer supported.) I suggest re-installing NetBox per [the official documentation](https://netbox.readthedocs.io/en/stable/installation/) to avoid such issues. If you continue to encounter problems after reinstallation, please ask for assistance on [our mailing list](https://groups.google.com/forum/#!forum/netbox-discuss).
Author
Owner

@liaocyintl-deprecated commented on GitHub (Oct 7, 2020):

Hi,
Thanks for your reply. I will try these solutions.
Thank you very much.

@liaocyintl-deprecated commented on GitHub (Oct 7, 2020): Hi, Thanks for your reply. I will try these solutions. Thank you very much.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4166