Skip to content

Instantly share code, notes, and snippets.

@guewen
Last active January 24, 2020 08:37
Show Gist options
  • Save guewen/8004914 to your computer and use it in GitHub Desktop.
Save guewen/8004914 to your computer and use it in GitHub Desktop.
Create a PostgreSQL cluster in a ramfs and start the server on this cluster.
#!/bin/bash
PORT=6432
# Create a ramfs
if [ -f /tmp/pg_on_ram ]
then
echo "/tmp/pg_on_ram already exists"
exit 1
fi
mkdir /tmp/pg_on_ram
sudo mount -t ramfs none /tmp/pg_on_ram
# Create a Postgres cluster
sudo chown -R postgres /tmp/pg_on_ram
sudo -u postgres /usr/lib/postgresql/9.1/bin/initdb /tmp/pg_on_ram
# change port to 6432
sudo sed -i -e "s/^#\?\s*\(port\s*=\s*\).*$/\1$PORT/" /tmp/pg_on_ram/postgresql.conf
# better config for in-memory database (http://rhaas.blogspot.ch/2010/06/postgresql-as-in-memory-only-database_24.html)
sudo sed -i -e "s/^#?\(fsync\s*=\s*\).*$/\1off/" \
-e "s/^#\?\s*\(synchronous_commit\s*=\s*\).*$/\1off/" \
-e "s/^#\?\s*\(full_page_writes\s*=\s*\).*$/\1off/" \
-e "s/^#\?\s*\(bgwriter_lru_maxpages\s*=\s*\).*$/\10/" \
/tmp/pg_on_ram/postgresql.conf
sudo su postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl -D /tmp/pg_on_ram/ -l /tmp/pg_on_ram/postgresql.log start"
PID=$(sudo head -n 1 /tmp/pg_on_ram/postmaster.pid)
echo "postgresql started with PID $PID on port $PORT"
sudo tail -f /tmp/pg_on_ram/postgresql.log
sudo su postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl -D /tmp/pg_on_ram/ stop -m fast"
# Clean
sudo umount /tmp/pg_on_ram
sudo rm -rf /tmp/pg_on_ram
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment