Skip to main content

PG Docker

A python package to provide containerized postgres databases in python

Why would you need this?

If you are using postgres and want to write tests that run against a real database, then this will make your life easier.

Installation

Install via pip:

pip install pg-docker

You will also need to have docker.

Usage

Note: This package is mainly built with pytest in mind, but you can use the context managers documented below with other testing frameworks as well.

Example

With pytest:

import psycopg2


pytest_plugins = ["pg_docker"]


def test_using_a_database(pg_database):
    db_connection = psycopg2.connect(**pg_database.connection_kwargs())
    cursor = db_connection.cursor()
    cursor.execute("SELECT 'hello world!'")

    assert cursor.fetchone() == ("hello world!",)

Usage with pytest

You first need to enable the plugin. To do this add a conftest.py to the root directory of your tests and add:

pytest_plugins = ["pg_docker"]

You can find more details on how to activate plugins in the pytest docs

The plugin The following fixtures:

  • pg_database: DatabaseParams for a clean database.
  • pg_database_pool: A DatabasePool instance. Use this if you need more than one database in your tests at a time.

Configuring Database Migrations

Use the below template in your conftest.py to configure how your databases are set up.

def setup_db(pg_params):
    """Add any setup logic for your database in here."""
    pass

@pytest.fixture(scope="session")
def pg_setup_db():
    return setup_db

Note: You might be inclined to edit the above code to nest the setup_db function inside of the fixture function. This will not work, because the fixture result needs to be pickleable!

Advanced Usage (and other testing frameworks)

For other use cases you can use the database_pool context manager:

with database_pool() as db_pool:
    with db_pool.database as db_params:
        connection = psycopg2.connect(**db_params.connection_kwargs())

Metadata

Release files for pg-docker 0.11.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pg-docker 0.11.0
File Size Uploaded Provenance Actions
pg_docker-0.11.0.tar.gz 5.5 kB Without provenance Details for pg_docker-0.11.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pg-docker 0.11.0
File Interpreter ABI Platform Actions
pg_docker-0.11.0-py3-none-any.whl Python 3 none any Details for pg_docker-0.11.0-py3-none-any.whl

Total release size: 11.5 kB

Release files / pg_docker-0.11.0.tar.gz

Download URL pg_docker-0.11.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
424ed2d09f8b69c80a60caed0327dae6cc5334c6bb1253db5ca28938894310b7
BLAKE2b-256 checksum
How to use checksums
90e145e50af9dd70bb5e34d4680a5c97c84b0b4e86424334951a22860f424156
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.11.0 Darwin/22.3.0

Release files / pg_docker-0.11.0-py3-none-any.whl

Download URL pg_docker-0.11.0-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b47e88cfc6f7dbaa4ed8bf081e861b3f44d4dc341dabd943fdad2e2c43a0cccf
BLAKE2b-256 checksum
How to use checksums
081f4d31789838ee01536e91831248ce2447d2499db43650c939a59ccb0c21cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.2 CPython/3.11.0 Darwin/22.3.0

Security for pg-docker 0.11.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page