-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM postgres:12.20-bullseye@sha256:e1c0ba2f2a0bb8d1976c904d55ff7c817fcd5e922a938a05bb1698a6688028dd | ||
|
||
USER root | ||
|
||
ENV POSTGRES_USER=postgres | ||
ENV POSTGRES_PASSWORD=postgres | ||
ENV POSTGRES_DB=postgres | ||
|
||
ENV CONTAINER_USER=myuser | ||
ENV CONTAINER_USER_PASSWORD=mypassword | ||
ENV CONTAINER_USER_DB=mydb | ||
|
||
COPY --chown=postgres:postgres docker-entrypoint-initdb.d /docker-entrypoint-initdb.d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# TRE PostgreSQL example | ||
|
||
## Running | ||
|
||
This example shows how to set up basic postgres parameters, create/execute custom scripts to initialise a new database and run postgres. | ||
|
||
Create a directory to store the database files: | ||
|
||
```console | ||
mkdir /safe_data/<proj-id>/postgres | ||
sudo chown 10001:10001 /safe_data/<proj-id>/postgres | ||
``` | ||
|
||
Create an options file, `opts.txt`: | ||
|
||
```console | ||
-v /safe_data/<proj-id>/postgres:/var/lib/postgresql/data | ||
-p 5432:5432 | ||
``` | ||
|
||
Run with `ces-run`: | ||
|
||
```console | ||
ces-run --opt-file opts.txt ghcr.io/... | ||
``` | ||
|
||
## Notes | ||
|
||
From the [docker-postgres](https://github.com/docker-library/docs/blob/master/postgres/README.md) documentation (`Arbitrary --user Notes` section), the user running postgres has to: | ||
1. Be the owner of `/var/lib/postgresql/data` | ||
2. Exist in `/etc/passwd` | ||
|
||
To persist the database using the postgres image, a host directory or volume needs a bind-mount into `/var/lib/postgres/data` in the container. For this to work, the host directory that we use for the bind mount has to be empty on the first run and owned by the user that initialises the database, hence the `chown 10001:10001` command in the example above. |
10 changes: 10 additions & 0 deletions
10
rocker/postgres/docker-entrypoint-initdb.d/10_setup_test_database.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
|
||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL | ||
CREATE ROLE "$CONTAINER_USER" NOSUPERUSER CREATEDB CREATEROLE LOGIN PASSWORD '$CONTAINER_USER_PASSWORD'; | ||
CREATE DATABASE "$CONTAINER_USER_DB" OWNER "$CONTAINER_USER" ENCODING 'utf-8'; | ||
EOSQL | ||
|
||
# can import a database from backup here using pg_restore | ||
# or copy .sql file in docker-entrypoint-initdb for execution on startup |