How to connect to a remote postgres server using db.connect?

Thanks for the details and analysis – much appreciated! (Disclaimer: We’re not exactly database experts, so sorry if this is still a bit rough. We’ve also been meaning to include the uncompiled source of Prodigy’s database module to make this a little more transparent and easier to customise.)

For PostgreSQL, the database settings can include any available psycopg2 connection parameters – which seem to also allow a port and a host. So in the best case scenario, this might even be enough to make it work with a remote server.

Another missing piece: Prodigy uses the peewee package for its ORM, so anything peewee can do, we can potentially make happen in Prodigy. The section on PostgreSQL also mentions the playhouse extension which we could integrate, if it includes additional features that are useful or necessary to make Prodigy work for different configurations. (Btw, when looking at the peewee docs, keep in mind that peewee’s connect() != Prodigy’s connect(). Maybe the naming we choose here is bad, but we liked the simplicity and then again, in the DB world, everything is called “connect” anyways…)

Prodigy’s db.connect() doesn’t actually do that much – it takes care of some basic validation, initialises the respective database and returns an instance of the database class, prodigy.components.db.Database. The Database class is currently initialised with the database ID, e.g. “postgresql”, and the database settings, and then resolves that to the respective database module provided by peewee, e.g. peewee.PostgresqlDatabase.

To be honest, I just realised that this is actually very bad default behaviour. Instead, db.Database should take the instance of the database, and all the validation, mapping of the arbitrary database ID etc. should happen in the connect() helper. This would let you plug in your own PostgresqlDatabase instance – e.g. a modified version of the peewee default, or the PostgresqlExtDatabase version provided by the playhouse package. Will fix this for the next release! :+1:


Edit: Just made some changes to the database handling, so in the upcoming version of Prodigy, you’ll be able to do something like this:

from prodigy.components.db import Database
from playhouse.postgres_ext import PostgresqlExtDatabase

psql_db = PostgresqlExtDatabase('my_database', user='postgres')
db = Database(psql_db, 'postgresql', 'Custom PostgreSQL Database')

Instead of the ID and settings, the Database class will then take an instance of peewee.Database (i.e. any of the classes provided by peewee or its extension package Playhouse). The other arguments are an optional display_id and display_name, mostly used for logging. They default to 'custom' and the function/class name or repr(db) for custom databases.

1 Like