blob: ba3c5732b3b0db314766f1958b65871eb5f3cd5c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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).
|
for my tech blog and contact information.