PostgreSQL in RAM: Blazing Fast

postgresql
linux
Author

Christophe Beaucé

Published

August 23, 2025

While working with Mark Burgess’ excellent SSTorytime project, I discovered how to run PostgreSQL entirely in memory instead of on disk.

The process is straightforward:

  1. Create a new data folder and mount it as a memory filesystem
  2. Grant access rights to your postgres user
  3. Stop the default postgres system service
  4. Start postgres manually using the memory filesystem for data storage
sudo mkdir -p /mnt/pg_ram
sudo mount -t tmpfs -o size=900M tmpfs /mnt/pg_ram
sudo chown postgres:postgres /mnt/pg_ram
sudo systemctl stop postgres
sudo -u postgres /usr/lib/postgresql/<version>/bin/initdb -D /mnt/pg_ram/pgdata
sudo -u postgres /usr/lib/postgresql/<version>/bin/pg_ctl -D /mnt/pg_ram/pgdata -l /mnt/pg_ram/logfile start

Note that all database data is lost when the system restarts. However, this configuration is perfect for SSTorytime, where the database serves as a frequently refreshed tool and the N4L files remain the source of truth. Data uploads and searches are now blazingly fast on my laptop.

Here’s the pull request that was merged.