/django-postgrespool2

Postgres Connection Pooling for Django, powered by SQLAlchemy.

Primary LanguagePythonMIT LicenseMIT

PyPI - Django Version PyPI - License PyPI

Django-PostgresPool2

This is simple PostgreSQL connection pooling for Django. You can use it as an alternative for PgBouncer. This is a fork of the original django-postgrespool.

Installation

Installing Django-PostgresPool2 is simple, with pip:

$ pip install django-postgrespool2

Usage

Using Django-PostgresPool2 is simple, just set django_postgrespool2 as your connection engine:

DATABASES = {
    "default": {
        "ENGINE": "django_postgrespool2",
        "NAME": "yourdb",
        "USER": "user",
        "PASSWORD": "some_password",
        "HOST": "localhost",
    }
}

If you're using the environ module:

import environ

env = environ.Env()

DATABASES = {"default": env.db("DATABASE_URL", engine="django_postgrespool2")}

Everything should work as expected.

Configuration

Optionally, you can provide pool class to construct the pool (default sqlalchemy.pool.QueuePool) or additional options to pass to SQLAlchemy's pool creation. List of possible values DATABASE_POOL_CLASS is here

DATABASE_POOL_CLASS = 'sqlalchemy.pool.QueuePool'

DATABASE_POOL_ARGS = {
    'max_overflow': 10,
    'pool_size': 5,
    'recycle': 300,
}

Here's a basic explanation of two of these options:

  • pool_size – The minimum number of connections to maintain in the pool.
  • max_overflow – The maximum overflow size of the pool. This is not the maximum size of the pool.
  • recycle - Number of seconds between connection recycling, which means upon checkout, if this timeout is surpassed the connection will be closed and replaced with a newly opened connection.

The total number of "sleeping" connections the pool will allow is pool_size. The total simultaneous connections the pool will allow is pool_size + max_overflow.

As an example, databases in the Heroku Postgres starter tier have a maximum connection limit of 20. In that case your pool_size and max_overflow, when combined, should not exceed 20.

Check out the official SQLAlchemy Connection Pooling docs to learn more about the optoins that can be defined in DATABASE_POOL_ARGS.