I would find the initdb executable and use it to create a new temporary storage for the database instance writable by the current user. Since this is a test instance, use something like initdb --auth=trust --username=postgres -D /path/to/temp/datadir , so the new database is configured to accept connections without the need for passwords.
Use pg_ctl to start the server by specifying a port to override the default value in the generated postgresql.conf and avoid conflicts.
Connect to the new database cluster and do any work. First you need to log in as the postgres user and run any required CREATE USER and CREATE DATABASE commands before transferring control of your test code.
Finally, use pg_ctl to stop it and finally delete the data directory.
All you need is initdb and pg_ctl in PATH and a helper class for managing the server.
Cm:
Craig Ringer
source share