Pip install Postgres – no Docker/Brew/apt

leontrolski1 pts1 comments

GitHub - leontrolski/postgresql-testing: Postgresql testing for Python/Pytest · GitHub

/" data-turbo-transient="true" />

Skip to content

Search or jump to...

Search code, repositories, users, issues, pull requests...

-->

Search

Clear

Search syntax tips

Provide feedback

--><br>We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Cancel

Submit feedback

Saved searches

Use saved searches to filter your results more quickly

-->

Name

Query

To see all available qualifiers, see our documentation.

Cancel

Create saved search

Sign in

/;ref_cta:Sign up;ref_loc:header logged out"}"<br>Sign up

Appearance settings

Resetting focus

You signed in with another tab or window. Reload to refresh your session.<br>You signed out in another tab or window. Reload to refresh your session.<br>You switched accounts on another tab or window. Reload to refresh your session.

Dismiss alert

{{ message }}

leontrolski

postgresql-testing

Public

Notifications<br>You must be signed in to change notification settings

Fork

Star

main

BranchesTags

Go to file

CodeOpen more actions menu

Folders and files<br>NameNameLast commit message<br>Last commit date<br>Latest commit

History<br>12 Commits<br>12 Commits

.github/workflows

.github/workflows

src/postgresql_testing

src/postgresql_testing

tests

tests

.gitignore

.gitignore

.python-version

.python-version

LICENSE

LICENSE

README.md

README.md

pyproject.toml

pyproject.toml

uv.lock

uv.lock

View all files

Repository files navigation

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:

Iterator[str]:<br>config = postgresql_testing.DatabaseConfig.default("testing-db")<br>postgresql_testing.initdb(config.directory, on_existing="use")<br>with postgresql_testing.serve(config):<br>postgresql_testing.ensure_user(config)<br>postgresql_testing.create_database(config, on_existing="replace")<br>yield config.dsn">import postgresql_testing

@pytest.fixture(scope="session")<br>def db() -> Iterator[str]:<br>config = postgresql_testing.DatabaseConfig.default("testing-db")<br>postgresql_testing.initdb(config.directory, on_existing="use")<br>with postgresql_testing.serve(config):<br>postgresql_testing.ensure_user(config)<br>postgresql_testing.create_database(config, on_existing="replace")<br>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

Bundled for good measure is a local-first copy of explain.dalibo.com

Get your query plan by:

EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON) SELECT ...

Then copy and:

pbpaste | postgresql-testing-explain

There are a couple of helpers for creating/using template dbs and archives. I have some vague long term plan for some kind of "Docker layers for migrated databases" with clever caching (or not), but I'm not quite sure what it looks like yet.

Some benchmarking:

number of tables<br>create from template<br>dump to archive<br>create from archive

100<br>80ms<br>80ms (0.2MB)<br>150ms

1000<br>500ms<br>300ms (2MB)<br>1100ms

On macos using a ramdisk, it is slightly quicker:

number of tables<br>create from template<br>dump to archive<br>create from archive

100<br>70ms<br>70ms<br>120ms

1000<br>350ms<br>300ms<br>1000ms

See claims/advice from planet Go.

About<br>Postgresql testing for Python/Pytest<br>Resources<br>Readme<br>MIT license<br>Activity<br>Stars<br>0 stars<br>Watchers<br>0 watching<br>Forks<br>0 forks<br>Report repository

Releases

Packages

Contributors

Languages

You can’t perform that action at this time.

testing postgresql_testing config postgresql from python

Related Articles