PostgreSQL is not a memory server. It cannot be loaded only as a library and started.
You will need a wiring harness control code:
initdb temporary datadir
Edit postgresql.conf , as required by the addition of lines, or by including include_dir and drop-down files. If you only need a trivial configuration, you can skip this step; things like port can be set via environment variables, and many others can be set using the -c flags in the server start command.
create a random port number to use
Start the server - run postgres -D the_datadir new instance of the server using the Java Process control. You can pass custom configuration values ββwith -c and also set environment variables to control behavior.
Connect to JDBC and CREATE DATABASE , then run the test setup
Run your tests
Stop the server by killing the process you started.
Remove datadir
You will need to control the process in the class with the appropriate temp directory processing, cleaning up on an unclean output (kill the Pg server and remove the datadir for exception), etc.
I will be surprised if you cannot find a canned code for the harness for borrowing for this. I wonder if we add to PgJDBC and associate it with the driver? If you find something good or write something good, please write me a comment on this answer and I will consider it to be included as a utility class in PgJDBC.
However, tests are much more common in a newly created test database in an existing PostgreSQL instance that is already running on your server. You simply configure your test suite using the PostgreSQL username / password / host / port / database (or let it connect to the postgres database and the CREATE and DROP database). In the end, some configuration is required: installing or compiling PostgreSQL, make sure the binaries are on PATH , etc.
Another option is to use Amazon RDS: use the AWS RDS API to start a new instance of PostgreSQL and use it for your tests. This is probably impractical if your tests fail to run for a long time due to installation time and minimal runtime, but this is another option similar to the service in Heroku.
Craig Ringer Jun 04 '14 at 11:32 2014-06-04 11:32
source share