https://github.com/cloudblue/django-postgresql-reconnect
PostgreSQL database reconnection helper
https://github.com/cloudblue/django-postgresql-reconnect
Last synced: 5 months ago
JSON representation
PostgreSQL database reconnection helper
- Host: GitHub
- URL: https://github.com/cloudblue/django-postgresql-reconnect
- Owner: cloudblue
- License: apache-2.0
- Created: 2022-07-27T09:37:30.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2025-06-23T12:11:58.000Z (12 months ago)
- Last Synced: 2025-09-27T18:02:45.648Z (9 months ago)
- Language: Python
- Size: 49.8 KB
- Stars: 9
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Django PostgreSQL backend with reconnection support

[](https://pypi.org/project/ddjango-postgresql-reconnect/)
[](https://readthedocs.org/projects/django-postgresql-reconnect)
[](https://sonarcloud.io/dashboard?id=django-postgresql-reconnect)
[](https://pypi.python.org/pypi/django-postgresql-reconnect/)
[](https://sonarcloud.io/dashboard?id=django-postgresql-reconnect)
[](https://pypi.org/project/django-postgresql-reconnect/)
## Introduction
In some cases (network connection lost, load balancer issues) the connection to the database is lost without giving Django a notification.
Django tries to use such a connection and gets errors because of it. For a correct recovery, we need to restart the application workers.
## Install
```
$ pip install django-postgresql-reconnect
```
## Integration
### Handling connection issues when processing HTTP requests
```python
# settings.py
DATABASES = {
# The database back end that performs reconnect when the connection is closed
'default': {
'ENGINE': 'django_postgresql_reconnect.backend',
'NAME': 'db_name',
'USER': 'db_user',
'PASSWORD': 'pwd',
'HOST': 'db_host',
'RECONNECT': True,
},
}
MIDDLEWARE = [
# Middleware checks and reconnects before processing requests
'django_postgresql_reconnect.middleware',
...
]
```
### Decorator for using in other places.
For example, in management commands.
```python
import django_postgresql_reconnect
@django_postgresql_reconnect.decorator
def some_awesome_function():
...
```
## Testing
1. Python 3.8+
0. Install dependencies `pip install poetry && poetry install`
Check code style: `poetry run flake8`
Run tests: `poetry run pytest`
Tests reports are generated in `tests/reports`.
* `out.xml` - JUnit test results
* `coverage.xml` - Coverage xml results
To generate HTML coverage reports use:
`--cov-report html:tests/reports/cov_html`
## License
`Django PostgreSQL Reconnect` is released under the [Apache License Version 2.0](https://www.apache.org/licenses/LICENSE-2.0).