(translation of my answer from Using PostgreSQL in memory and its generalization):
You cannot run Pg in-process, in-memory
I cannot figure out how to start the Postgres database in memory for testing. Is it possible?
No, It is Immpossible. PostgreSQL is implemented in C and compiled into platform code. Unlike H2 or Derby, you cannot just load the jar and run it as a database with backup storage.
Unlike SQLite, which is also written in C and compiled into platform code, PostgreSQL cannot be loaded in the process. It requires several processes (one per connection), since it is a multi-processor rather than multi-threaded architecture. The requirement of multiprocessing means that you must run postmaster as a separate process.
Instead: preconfigure the connection
I suggest just writing your tests to expect some hostname / username / password to work, and have a test posting of the CREATE DATABASE database with the drop, and then DROP DATABASE at the end of the run. Get data about connecting to the database from the properties file, create target properties, an environment variable, etc.
It is safe to use an existing instance of PostgreSQL in which you already have the databases you care about until the user you supply for your unit tests is not a superuser, but only a user with CREATEDB . In the worst case scenario, you will create performance problems in other databases. For this reason, I prefer to run a fully isolated PostgreSQL installation for testing.
Instead: launching a PostgreSQL instance for testing
Alternatively, if you are really interested, you can install the test postings of the binaries initdb and postgres , run initdb to create the database, change pg_hba.conf trust , run postgres to run it on a random port, create a user, create a database and run tests . You could even link the PostgreSQL binaries for several architectures in the bank and unpack those for the current architecture into a temporary directory before running the tests.
Personally, I believe that great pain should be avoided; The easiest way to set up a test database. However, with the advent of include_dir support in postgresql.conf it got a little easier; now you can just add one line and then write the generated configuration file for the rest.
Faster testing with PostgreSQL
For more information on how to safely improve PostgreSQL performance for testing purposes, see the detailed answer I wrote earlier: Optimize PostgreSQL for quick testing.
H2 PostgreSQL dialog is not a true substitute
Some people instead use the H2 database in the PostgreSQL dialog mode to run tests. I think this is almost as bad as the Rails people using SQLite for testing and PostgreSQL for production deployments.
H2 supports some PostgreSQL extensions and emulates a PostgreSQL dialect. However, this is just emulation. You will find areas where H2 accepts the query, but PostgreSQL does not, where the behavior is different, etc. . You will also find many places where PostgreSQL supports the execution of something that H2 simply cannot - as a window function, at the time of writing.
If you understand the limitations of this approach and access to the database is simple, H2 might be fine. But in this case, you are probably the best candidate for ORM, which abstracts the database because you still do not use its interesting functions - in which case you no longer need to worry about database compatibility.
Table spaces are not the answer!
Do not use table space to create an in-memory database. Not only is this not necessary, as it will not affect performance in any way, but it is also a great way to disrupt access to any other that you might need in the same PostgreSQL installation. Documentation 9.4 now contains the following warning :
WARNING
Despite the fact that it is located outside the PostgreSQL main data directory, table spaces are an integral part of the database cluster and cannot be considered as a stand-alone set of data files. They depend on the metadata contained in the main data directory and, therefore, cannot be attached to another database cluster or reinforced individually. Similarly, if you lose table space (file deletion, disk failure, etc.), the database cluster may become unreadable or may not start. Placing table space on a temporary file system, such as ramdisk risk, reliability of the entire cluster.
because I noticed that too many people are doing this and are having difficulty.
(If you did this, you can mkdir missing tablespace directory to start PostgreSQL, and then DROP missing databases, tables, etc. It’s better not to do this.)