Skip to content
Tech News
← Back to articles

Pip install Postgres – no Docker/Brew/apt

read original more articles
Why This Matters

This article introduces a lightweight Python tool for testing PostgreSQL databases without relying on Docker, Brew, or APT, simplifying setup and improving testing efficiency for developers. It enables quick creation of fresh databases, supports detailed query analysis, and offers flexible options for database templates and archives, making it highly valuable for developers and testers seeking streamlined database management during development cycles.

Key Takeaways

Postgresql Testing

Simple Postgres helpers for testing with Python - no docker, brew, apt, etc - uses postgresql-binaries. The interface is simple, but close enough to the metal that you can use it to eg. have a new database per testing thread/use a fresh database from a TEMPLATE or archive.

pip install ' postgresql-testing[15] '

Then to use with pytest, eg:

import postgresql_testing @ pytest . fixture ( scope = "session" ) def db () -> Iterator [ str ]: config = postgresql_testing . DatabaseConfig . default ( "testing-db" ) postgresql_testing . initdb ( config . directory , on_existing = "use" ) with postgresql_testing . serve ( config ): postgresql_testing . ensure_user ( config ) postgresql_testing . create_database ( config , on_existing = "replace" ) yield config . dsn

There are various useful flags and things - the source code is short enough to just dive in.

You can spin up a fresh db for immediate testing with:

postgresql-testing-serve postgres://testing:@localhost:8421/my-db

Or if you want a throwaway Postgres from anywhere:

uvx --from ' postgresql-testing[16] ' postgresql-testing-serve

... continue reading