--- title: "Temporary PostgreSQL server" description: "A shell script to run the PostgreSQL server off a temporary directory." published: 2020-10-14 --- 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](https://www.johbo.com/2017/on-demand-postgresql-for-your-development-environment.html) by Johannes Bornhold that reminded me of Unix' simplicity. You can download it [here](/files/temp-postgres.sh). 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](https://nixos.org/), simply replace the shebang on the first line of the script with the following two lines. ```sh #! /usr/bin/env nix-shell #! nix-shell --pure --packages postgresql -i bash ``` In case of [GNU Guix](https://guix.gnu.org/), use the following shebang. Please note, however, that the `env` command on your OS might not support the used `-S` argument. ```sh #! /usr/bin/env -S guix environment --pure --ad-hoc postgresql bash coreutils -- bash ``` You can find the full shell script [here](/files/temp-postgres.sh).