Skip to main content

pg_temp

pg_temp build status

Provides a simple class for creating a temporary userland Postgres db server.

The TempDB() class constructor will set up the database server in a temporary directory. If any databases are specified they are created inside the newly created server. The class provides a cleanup() method for stopping the server and disposing of the temporary files.

The module also provides an init_temp_db() function for managing the TempDB class as a singleton. This is useful for scenarios where you want to import in more than one module, but ensure that they all use the same database (and that the server and database are only created once). For example, this is handy in unit test code where you want to be able to test a single file or use a test runner to run tests in multiple files with only a single database.

init_temp_db() also registers an atexit handler to cleanup the temp database when the process exits.

Examples:

Create a temporary server with a database called 'testdb':

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')

# ... do stuff...

temp_db.cleanup()

Alternatively, useful in a test runner:

import pg_temp
temp_db = pg_temp.init_temp_db(databases=['testdb'])
# repeat above in multiple modules

# you can connect to this database using temp_db's pg_socket_dir
connection = psycopg2.connect(host=temp_db.pg_socket_dir, database='testdb')

# ... do stuff...
# the db is automatically cleaned up when the process exits

Last an interactive example:

>>> import pg_temp
>>> import psycopg2
>>> temp_db = pg_temp.TempDB(databases=['testdb'])
Creating temp PG server... done
(Connect on: `psql -h /var/folders/d7/n3_h9vnn3w3bbmsnbdb73fmw0000gn/T/pg_tmp_OQMGwC/socket`)
>>> connection = psycopg2.connect(host=temp_db.pg_socket_dir, database='testdb')
>>> cur = connection.cursor()
# The rest is stolen from psycopg's documentation.  You get the idea...
>>> cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
>>> cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)",
... (100, "abc'def"))
>>> cur.execute("SELECT * FROM test;")
>>> cur.fetchone()
(1, 100, "abc'def")
>>> connection.close()
>>> temp_db.cleanup()

Development

Install dependencies for testing:

# Create a virtualenv
virtualenv venv

# Activate the virtualenv
. venv/bin/activate

# Install dependencies for testing
pip install -r requirements-dev.txt

Run the unit tests directly:

make test

To test against multiple Python versions, without needing to use Virtualenv directly, run the unit tests using tox:

make tox

Check code style using flake8 and black:

make check

Code is auto-formatted using black. Since this was done relatively recently, you should configure git to ignore the reformatting commit using the --ignore-rev or the --ignore-revs-file option to git blame, or configure this by: git config blame.ignoreRevsFile .git-blame-ignore-revs The ignore revisions file is called .git-blame-ignore-revs

Metadata

Release files for pg-temp 0.9.1

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-temp 0.9.1
File Size Uploaded Provenance Actions
pg_temp-0.9.1.tar.gz 7.1 kB Without provenance Details for pg_temp-0.9.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pg-temp 0.9.1
File Interpreter ABI Platform Actions
pg_temp-0.9.1-py3-none-any.whl Python 3 none any Details for pg_temp-0.9.1-py3-none-any.whl

Total release size: 14.5 kB

Release files / pg_temp-0.9.1.tar.gz

Download URL pg_temp-0.9.1.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e7f1523266b7be7b07173e3dd419a7943c7b9a55ad7da28cfb17092c0f4d43a1
BLAKE2b-256 checksum
How to use checksums
181142c84929fb1c1f216ea32aacb8e24adfe82020a3b97c8e8a5d0bceea5598
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.10

Release files / pg_temp-0.9.1-py3-none-any.whl

Download URL pg_temp-0.9.1-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1af31a28fc08f4548c7ec569fbad89c08a7aa421986d322f9cc9a44a71838e3a
BLAKE2b-256 checksum
How to use checksums
e774694a6d8ef83a8958704d756ab3edf4d50c2e4172e027d5f45068916fe9e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.10

Security for pg-temp 0.9.1

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