Temporary PostgreSQL server
Today I want to share a simple shell script to spin up a temporary PostgreSQL database. I’ve been using this script for two years and found it useful enough to publish it now. The script is inspired by a blog post by Johannes Bornhold that reminded me of Unix’ simplicity. You can download it here.
The script essentially performs seven steps:
- Create a temporary directory using 
mktemp - Initialize the directory using 
initdb - Serve the directory using 
postgres - Ensure the server is up using 
pg_isready - Create a database using 
createdb - Wait for a 
SIGINT - Remove the temporary directory
 
Obviously, you still need to install PostgreSQL to use the script. However, you may use your favorite functional package manager to install PostgreSQL on-the-fly if you like. In case of Nix, simply replace the shebang on the first line of the script with the following two lines.
#! /usr/bin/env nix-shell
#! nix-shell --pure --packages postgresql -i bashIn case of GNU Guix, use the following shebang.
Please note, however, that the env command on your OS might not support the used -S argument.
#! /usr/bin/env -S guix environment --pure --ad-hoc postgresql bash coreutils -- bashYou can find the full shell script here.