summaryrefslogtreecommitdiff
path: root/_drafts/temporary-postgresql-server.md
diff options
context:
space:
mode:
authorStefan Kreutz <mail@skreutz.com>2020-07-06 22:40:21 +0200
committerStefan Kreutz <mail@skreutz.com>2020-07-06 22:40:21 +0200
commit07c1d9f590e8de064e9b527c3d425eb898f7e59e (patch)
tree38307806182a481ca534e853a8ba2c3db9596a0c /_drafts/temporary-postgresql-server.md
downloadblog-07c1d9f590e8de064e9b527c3d425eb898f7e59e.tar
Add initial version
This commit adds the first published version of the website including the first blog post, Unix Domain Socket Forwarding with OpenSSH.
Diffstat (limited to '_drafts/temporary-postgresql-server.md')
-rw-r--r--_drafts/temporary-postgresql-server.md100
1 files changed, 100 insertions, 0 deletions
diff --git a/_drafts/temporary-postgresql-server.md b/_drafts/temporary-postgresql-server.md
new file mode 100644
index 0000000..a46cd86
--- /dev/null
+++ b/_drafts/temporary-postgresql-server.md
@@ -0,0 +1,100 @@
+---
+title: "Temporary PostgreSQL server"
+description: "A simple shell script to run the PostgreSQL server off a temporary directory."
+published: 2020-06-02
+---
+
+Sometimes I need to spin up a local PostgreSQL server for one-off purposes.
+In these cases, I don't particularly like to either configure PostgreSQL manually or use a pre-configured Docker image because I experience this as overkill and inaccessible respectively.
+Instead, I use the simple shell script below to run the PostgreSQL server off a temporary directory until I decide to `kill` it.
+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.
+
+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 the [Nix package manager](https://nixos.org/nix/) to install PostgreSQL _on-the-fly_ and have it removed too, if you are into this.
+Simply put the following shebang in front of the script.
+
+```sh
+#! /usr/bin/env nix-shell
+#! nix-shell --pure --packages postgresql -i bash
+```
+
+Here is the full script with minimal error handling.
+
+```sh
+#! /bin/sh
+
+# temp_postgres runs a PostgreSQL server with a temporary data directory until
+# it receives a SIGINT.
+
+set -o nounset
+
+# Remove the temporary directory before exiting
+trap 'quit' INT
+quit() {
+ code="${1:-0}"
+ trap '' INT TERM
+ kill -TERM 0
+ wait
+ rm -rf "${tmpdir-}" || {
+ >&2 printf "temp_postgres: failed to remove temporary directory \"%s\"\\n" "${tmpdir}"
+ [ "${code}" -ne 0 ] || code=1
+ }
+ exit "${code}"
+}
+
+# Parse arguments
+[ $# -eq 2 ] || {
+ >&2 printf "temp_postgres: invalid arguments\\n"
+ printf "Usage: temp_postgres <dbname> <username>\\n"
+ quit 1
+}
+dbname="$1"
+username="$2"
+
+# Create a temporary directory
+tmpdir="$( mktemp --directory )" || {
+ >&2 printf "temp_postgres: failed to create temporary directory\\n"
+ quit 1
+}
+
+# Initialize the directory
+initdb --pgdata="${tmpdir}" --username="${username}" || {
+ >&2 printf "temp_postgres: failed to initialize database\\n"
+ quit 1
+}
+
+# Serve the directory
+( postgres -k "${tmpdir}" -D "${tmpdir}" </dev/null ) &
+
+# Test the connection
+sleep 1
+pg_isready --host="${tmpdir}" --dbname="postgres" --username="${username}" --timeout=10 || {
+ >&2 printf "temp_postgres: failed to connect to server\\n"
+ quit 1
+}
+
+# Create a database
+createdb --host="${tmpdir}" --username="${username}" --no-password "${dbname}" || {
+ >&2 printf "temp_postgres: failed to create database\\n"
+ quit 1
+}
+
+printf '
+Connect with the following command:
+
+\tpsql --host=localhost "%s" "%s"
+
+' "${dbname}" "${username}"
+
+wait
+```
Generated by cgit. See skreutz.com for my tech blog and contact information.