Peewee and Postgres HA: load balancer or master/master fail-over?
I'm not familiar with that project, but I think I get the jist of your question and hopefully can provide some pointers.

Peewee implements a simple connection-per-thread model internally. The connection pool simply plugs into the connect / close steps of the usual peewee workflow to recycle connections. So it provides pooling, but not load-balancing or master/slave or anything of that nature. The pool's `_connect()` method shows how you can insert your own logic for actually creating connections.

Maybe the simplest thing you could do would be to skip the pool for now and just subclass `PostgresqlDatabase` and override the `_connect()` method (the method responsible for actually creating the psycopg2 connection). You could pass in a list of hosts and then, when the method is called, open a connection on the appropriate host. You could also add your failover logic here, e.g.:

```python

class ExampleDatabase(PostgresqlDatabase):
    def __init__(self, database, conn_info_list, **kwargs):
         # e.g., [{'host': '10.8.0.1'}, {'host': '10.8.1.1'}, ...]
         self.conn_info_list = conn_info_list
         self.conns = itertools.cycle(self.conn_info_list)
         super(ExampleDatabase, self).__init__(database, **kwargs)

    def _connect(self):
        while self.conn_info_list:
            conn_kwargs = next(self.conns)  # load balancing
            try:
                kwargs = self.connect_params.copy()  # Get a copy of anything specified in __init__()
                kwargs.update(conn_kwargs)  # Add in args specific for the next conn in list.
                conn = psycopg2.connect(database=self.database, **kwargs)
            except SomeException:
                # failover
                self.conn_info_list.remove(conn_kwargs)  # Remove the bad conn from list
                self.conns = itertools.cycle(self.conn_info_list)
            else:
                return conn
        raise Exception('out of connections to try! everything failed!')
```

Hopefully this gives you some ideas, though I think stuff like this might be better handled in a dedicated connection proxy, e.g. pgbouncer or something that sits between peewee and your database cluster. 1. Thank you for the code snippet - that is what I had in mind. Though hoped `peewee` had more hidden treasures right in the play box :-)

2. `PgPool` and `PgBouncer` both add needless complexity, while their fail-over modes are rather limited/inflexible.

3. Thanks again!


@coleifer Charles:

1. say, there is a master/master replication cluster based on `pglogical`:
https://www.2ndquadrant.com/en/resources/pglogical/pglogical-docs/

2. question: is there an easy way to reuse [PooledPostgresqlExtDatabase](http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#PooledPostgresqlExtDatabase)
so that it can serve as:
* a round robin load balancer for the cluster
* a transparent master/master fail-over manager
* ?

3. any ideas are much appreciated, thank you.


