pg_temp for testing with postgres

141 views
Skip to first unread message

uok...@gmail.com

unread,
Aug 18, 2020, 4:21:23 PM8/18/20
to sqlalchemy

Hi all, thought I would post this here since this community might find this useful.

I've created a simple python module which can be used to quickly spin up a temporary, disposable postgres server. The main purpose I envisioned for this was for writing tests.


Basically, it works like this:
from pg_temp import TempDB
temp_db = TempDB(databases=['testdb'])
# you can connect to this database using temp_db's pg_socket_dir
connection = psycopg2.connect(host=temp_db.pg_socket_dir, database='testdb')

For many years since using sqlalchemy to abstract the database, I had fallen into the common trap of writing production code targeting postgres, and running unit tests against sqlite, which is a bad idea for many reasons. So I wrote pg-temp so that I'd be able to run unit tests against postgres as easily as I had been doing so against sqlite.

I've been working on it for a couple of years already, so I'd appreciate any feedback or opinions you, the sqlalchemy community, might have. Thanks

Uri
Reply all
Reply to author
Forward
0 new messages