psycopg / psycopg2

PostgreSQL database adapter for the Python programming language
https://www.psycopg.org/
Other
3.35k stars 506 forks source link

Feature request: add connection_factory to AbstractConnectionPool #1625

Closed moretea closed 1 year ago

moretea commented 1 year ago

I'd like to use procastinate in my Django project, mainly because of it's awesome name ;).

I'm deploying my application on AWS, and would like to enable automatic secrets management for the RDS database that I'm using there. This implies that the database credentials can (and will) be rotated out, and that one should fetch fresh database credentials that have the newly applied password.

I have managed to do this by:

class RDSThreadedConnectionPool(ThreadedConnectionPool):
    def __init__(self, *args, **kwargs):
        self._secret_arn = kwargs.pop("secret_arn")
        super().__init__(*args, **kwargs)

    def _connect(self, *args, **kwargs):
        self._update_rds_credentials()
        super()._connect(*args, **kwargs)

     def _update_rds_credentials(self):
        secretsmanager = boto3.client("secretsmanager")
        secret = secretsmanager.get_secret_value(self._secret_arn)["SecretString"]
        self._kwargs["password"] =json.loads(secret)["password"]

I would much prefer to be able to pass a connection factory to the pool's constructor, like one can do for psycopg2.connect(), instead of overriding a private method.

dvarrazzo commented 1 year ago

The psycopg2 pool will not receive further development. Please move to psycopg 3, whose pool supports a connection class parameter.