That's weird...

Running your Django tests in memory with PostgreSQL

· Apostolis Bessas

Many people advise to use sqlite for running your tests in django. The reason is that Django will automatically create the database in-memory, which speeds up any tests that query the database.

However, this means that your test database is different than your production one, which might cause issues because of the differences between the databases, such as how they handle transactions. Besides, you might be using features of PostgreSQL that sqlite does not support.

There are ways to make PostgreSQL much faster for running your tests, but why not run your database in-memory as well and avoid any I/O operations?

This is possible in linux by using a ramdisk for your test database.

First, you need to create the ramdisk:

sudo mount -t ramfs none /mnt/
sudo mkdir /mnt/pgdata/
sudo chown postgres:postgres /mnt/pgdata/

Then, you need to create a tablespace that uses the ramdisk:

psql -d postgres -c "CREATE TABLESPACE ramfs LOCATION '/mnt/pgdata'"

The last step is to instruct django to use this tablespace for the test database. This can be done by adding the following to your settings:

if 'test' in sys.argv:
    DEFAULT_TABLESPACE = 'ramfs'

This will help reducing the running time of your test suite a bit more.

Keep in mind you might have to drop the tablespace and re-create it after a reboot.