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:
- Create a new data folder and mount it as a memory filesystem
- Grant access rights to your postgres user
- Stop the default postgres system service
- 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.