There's enough for everyone

गते गते पारगते पारसंगते बोधि स्वाहा गते गते पारगते पारसंगते बोधि स्वाहा

postgres on RAM FS for faster tests

One of the big slowdowns with postgres (and RDBMSs in general) is writing data to disk and ensuring data integrity. So to make that process faster, here’s a way to put a postgres db on a ram disk and turn off some of the data integrity in favour of faster operation.


So assuming /tmp has a ram fs of some kind mounted on it (tmpfs on linux, OSX has one, I’m not sure what it’s called or how to mount it):

1
2
3
4
5
6
7
8
9
10
export some_dir=/tmp/your_project_name

# so psql works nicely, in this terminal at least
export PGPORT=5858

# create the db cluster
pg_ctl init -D $some_dir

# start on a different port, with options for fast operation
pg_ctl -D $some_dir start -o "-c fsync=off -c synchronous_commit=off -c full_page_writes=off"

Now tell rails where to find your db. Edit config/database.yml to have

1
2
3
4
5
test:
  adapter: postgresql
  encoding: utf8
  database: <your_database_name_here>
  port: 5858 # <-- same as port above

… and the test setup dance

1
RAILS_ENV=test rake db:create db:migrate

Run tests fast(er). For fun say sudo iotop in another terminal to check that the postgres processes do not write data to disks. You might also want to create log/test.log as a link to a file on the ramfs:

1
2
3
rm log/test.log
ln -s /tmp/test.log log/
truncate -s0 /tmp/test.log

When you’re finished, say

1
2
pg_ctl -D $some_dir stop
rm -r $somedir

Comments