Skip to main content

Quickly create Postgres databases, e.g. for testing

Project description

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 env27

# Activate the virtualenv
. env27/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:

make check

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

pg_temp-0.7.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

pg_temp-0.7.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file pg_temp-0.7.1.tar.gz.

File metadata

  • Download URL: pg_temp-0.7.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.9

File hashes

Hashes for pg_temp-0.7.1.tar.gz
Algorithm Hash digest
SHA256 d6c1bf7e8a864f307511e2889d9a67dd70a6ebe848b9a78cf055973547c7ff3f
MD5 3acb859feee289f8d46f3cd81edca3be
BLAKE2b-256 16acf12a4f35f4e85fc2858cd57b13feb49375d2a29e90f996b8190b48bca656

See more details on using hashes here.

File details

Details for the file pg_temp-0.7.1-py3-none-any.whl.

File metadata

  • Download URL: pg_temp-0.7.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.9

File hashes

Hashes for pg_temp-0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6daf110239cc243e3e410684d6162bcc56535b925f9b33ab54dd8219ea5c79ae
MD5 8450ef2379afec287c9af8e93b497183
BLAKE2b-256 e12b5b3d80ba6c76466396039cd8839dd6aef8f49df23267981d10ef6a2e6d01

See more details on using hashes here.

Supported by

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