Create temporary postgres instances
Go to file
George Thomas 7f2467a6d6
Migrate to prettyprinter from deprecated ansi-wl-pprint (#279)
We replace the `text` function in most places in favour of just using the already-enabled `OverloadedStrings`, or with `pretty` when not using string literals.

Where the old instance `Pretty a => Pretty (Maybe a)` was relied upon, we replace `pretty` with `maybe mempty id` (then apply an HLint-suggested simplification), which is what we'd get from inlining a few definitions with the old library.
2023-08-08 10:33:28 -07:00
.github Create FUNDING.yml 2019-06-03 12:23:33 -07:00
benchmark remove slow benchmarks 2019-12-17 07:07:04 -08:00
blog move blog 2019-12-15 10:24:46 -08:00
profiling New cacheAction API #210 #211 (#213) 2019-12-08 11:43:58 -08:00
provision Remove SocketClass #137 and fix #138. One test fails on macos but not on linux 2019-11-22 06:36:26 -08:00
resource-soak-test fix withDbCache doc #244 2019-12-28 09:40:37 -08:00
s move blog 2019-12-15 10:24:46 -08:00
src/Database/Postgres Migrate to prettyprinter from deprecated ansi-wl-pprint (#279) 2023-08-08 10:33:28 -07:00
test Add a auto_explain config #229 2019-12-28 23:15:55 -08:00
.gitignore add dist-newstyle to gitignore 2019-11-10 07:17:31 -08:00
.travis.yml Remove SocketClass #137 and fix #138. One test fails on macos but not on linux 2019-11-22 06:36:26 -08:00
CHANGELOG.md Use TMP and TMPDIR #256 (#257) 2020-05-29 13:29:39 -07:00
developer-notes.md Add a auto_explain config #229 2019-12-28 23:15:55 -08:00
LICENSE first commit 2017-06-11 12:37:45 -04:00
README.md Adds github actions instructions (#270) 2020-10-30 20:22:27 -07:00
Setup.hs first commit 2017-06-11 12:37:45 -04:00
stack.yaml #167 apply hlint suggestions with better hlint refactor command 2019-11-27 06:55:04 -08:00
stack.yaml.lock #167 apply hlint suggestions with better hlint refactor command 2019-11-27 06:55:04 -08:00
tmp-postgres.cabal Migrate to prettyprinter from deprecated ansi-wl-pprint (#279) 2023-08-08 10:33:28 -07:00
TODO.md sketch out blogs 2019-12-05 06:01:11 -08:00
Vagrantfile Remove SocketClass #137 and fix #138. One test fails on macos but not on linux 2019-11-22 06:36:26 -08:00

Hackage Travis CI Status

tmp-postgres

tmp-postgres provides functions for creating a temporary postgres instance. By default it will create a temporary data directory and a temporary directory for a UNIX domain socket for postgres to listen on.

Here is an example using the exception-safe 'with' function:

with $ \db -> bracket
  (connectPostgreSQL (toConnectionString db))
  close $
  \conn -> execute_ conn "CREATE TABLE foo (id int)"

To extend or override the defaults use withConfig (or startConfig).

tmp-postgres ultimately calls initdb (optionally) , postgres and createdb (optionally).

All of the command line, environment variables and configuration files that are generated by default for the respective executables can be extended.

In general tmp-postgres is useful if you want a clean temporary postgres and do not want to worry about clashing with an existing postgres instance (or needing to ensure postgres is already running).

Here are some different use cases for tmp-postgres and their respective configurations:

  • The default 'with' and 'start' functions can be used to make a sandboxed temporary database for testing.
  • By disabling initdb one could run a temporary isolated postgres on a base backup to test a migration.
  • By using the 'stopPostgres' and 'withRestart' functions one can test backup strategies.

WARNING!! Ubuntu's PostgreSQL installation does not put initdb on the PATH. We need to add it manually. The necessary binaries are in the /usr/lib/postgresql/VERSION/bin/ directory, and should be added to the PATH

echo "export PATH=$PATH:/usr/lib/postgresql/VERSION/bin/" >> /home/ubuntu/.bashrc

Installation

macOS

$ brew install postgres
$ stack install tmp-postgres

Ubuntu

Ubuntu's PostgreSQL installation does not put initdb on the PATH. We need to add it manually.

$ sudo apt-get install postgresql-VERSION
$ echo "export PATH=$PATH:/usr/lib/postgresql/VERSION/bin/" >> /home/ubuntu/.bashrc
$ stack install tmp-postgres

GitHub Actions

Assuming you are using one of the standard workflow containers, simply add workflow steps which install postgresql as above. For example,

jobs:
  build:
    runs-on: ubuntu-18.04
    steps:
      - name: "Install postgresql"
        run: "sudo apt-get update && sudo apt-get install postgresql-10"

      # ...

      - name: "Run test"
        run: |
          export PATH=$PATH:/usr/lib/postgresql/10/bin
          stack test