Skip to main content

PostgreSQL testing helpers

Project description

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 'postgresql-binaries==18.*'

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

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 create from template dump to archive create from archive
100 80ms 80ms (0.2MB) 150ms
1000 500ms 300ms (2MB) 1100ms

On macos using a ramdisk, it is slightly quicker:

number of tables create from template dump to archive create from archive
100 70ms 70ms 120ms
1000 350ms 300ms 1000ms

See claims/advice from planet Go.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

postgresql_testing-0.0.7.tar.gz (288.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

postgresql_testing-0.0.7-py3-none-any.whl (289.4 kB view details)

Uploaded Python 3

File details

Details for the file postgresql_testing-0.0.7.tar.gz.

File metadata

  • Download URL: postgresql_testing-0.0.7.tar.gz
  • Upload date:
  • Size: 288.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for postgresql_testing-0.0.7.tar.gz
Algorithm Hash digest
SHA256 57222b3f2b307f09ff41b836b3045dd216f32bc23ab54102c1166da0093c5b5f
MD5 d56b42b38032b5286b107472e52e5526
BLAKE2b-256 142ead9726cacea57e89208a990f672c448cd2fee03a09a3c19e38f4758b40f9

See more details on using hashes here.

File details

Details for the file postgresql_testing-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: postgresql_testing-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 289.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for postgresql_testing-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 a78a14c6cc4beac0f1f9c3fdd424282c3ffa758bdea6b2a96b1ee6e1c4213be6
MD5 3bf3c86e57b7f7bacf8e9e8bd19b107f
BLAKE2b-256 fdc83b07c6e73069727f7a47bdf3b0951f94da54d07339399029ebbdcffbb9fc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page