diff options
author | Stefan Kreutz <mail@skreutz.com> | 2020-10-14 23:13:32 +0200 |
---|---|---|
committer | Stefan Kreutz <mail@skreutz.com> | 2020-10-14 23:13:32 +0200 |
commit | 1021b2dec462a9b83a27a39efcdbf73f9612e039 (patch) | |
tree | bbf69b7a7f112199ff72c69a237ffefbba04483d /posts/temporary-postgresql-server.md | |
parent | 39d1971cdce6e52bee7f3a965fc045d3e5df352a (diff) | |
download | blog-1021b2dec462a9b83a27a39efcdbf73f9612e039.tar |
Post Temporary PostgreSQL server
Diffstat (limited to 'posts/temporary-postgresql-server.md')
-rw-r--r-- | posts/temporary-postgresql-server.md | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/posts/temporary-postgresql-server.md b/posts/temporary-postgresql-server.md new file mode 100644 index 0000000..ba3c573 --- /dev/null +++ b/posts/temporary-postgresql-server.md @@ -0,0 +1,38 @@ +--- +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). |