bitbucket/overview | download 2.6 | man page
$ uri=$(pg_tmp) $ psql $uri -f schema.sql $ psql $uri
Tagwatch: using pg_tmp to facilitate application development
WEBM (31MB, vp8-vorbis) MP4 (27MB, h264-aac)
is a compact shell script designed to make unit testing, integration testing with
PostgreSQL easy in any language.
Sometimes SQLite is used as drop-in replacement for PostgreSQL when running tests in order to make tests self-contained and therefore free of side-effects. This technique seems to suffice for simple interactions, but it does so by reducing the set of features that an application can use to the functionality common to both platforms. PostgreSQL is not a heavy weight key-value store it is more like a specialized programming environment.
The engineering genius of unit tests is mainly the ability they have to shape the design of the internal interfaces used to build an application. To ensure that we are testing the module and not the environment we mock APIs to external resources, but never the runtime or the language itself. An application built on Postgres is an application where individual functions span more than process—and more than one runtime. Testing concretely requires verification of methods as complete “units”.
To facilitate the creation of tests that run autonomously and concurrently two libraries have been developed to start up and tear down a database in the test code itself: Test-postgresql (Perl), and testing.postgresql (Python). Both of these are well-maintained and highly configurable, but they have some limitations:
is less configurable from the command line, is impervious to deadlocks, requires
no external libraries, and is already tuned for the singular task of reducing the
startup time to less than a second. This makes a full-featured PostgreSQL database
useful for unit testing and other tests that require rapid feedback.
|Total Wait Time||Speedup||Strategy|
|7.2||—||Naive in-process creation and teardown|
Disabling runtime fsync (
Spinning to make the first connection (instead of
Background database initialization (
|1 second||1.3||Shut down database asynchronously|
On a production server, generous space for
is always a good idea, but an ephemeral instance has reason to limit space used on
To this end, the configuration installed by
allows the Postgres server clean up quickly by writing checkpoint after 64MB of
WAL (the default is 1GB).
In some cases it is useful to use
to construct a snapshot of larger database that is populated by
pg_dump | pg_restore
. Since PostgreSQL is already trimming WAL the on-disk representation is already