|
1 |
| -# docker-postgres |
2 |
| -PostgresSQL image with primary & replica support |
| 1 | +# PostgreSQL Dockerfile with primary and replica support |
| 2 | + |
| 3 | +PostgresSQL image with Primary & Replica support. In order to build the image, run: |
| 4 | + |
| 5 | +```bash |
| 6 | +DOCKER_REPO=ghcr.io/mutablelogic/docker-postgres make docker |
| 7 | +``` |
| 8 | + |
| 9 | +Replacing the `DOCKER_REPO` with the repository you want to use. |
| 10 | + |
| 11 | +## Environment variables |
| 12 | + |
| 13 | +The image can be run as a primary or replica, depending on the environment variables passed on the |
| 14 | +docker command line: |
| 15 | + |
| 16 | +* `POSTGRES_REPLICATION_PRIMARY`: **Required for replica** The host and port that the replica will use |
| 17 | + to connect to the primary, in the form `host=<hostname> port=5432`. When not set, |
| 18 | + the instance role is a primary. |
| 19 | +* `POSTGRES_REPLICATION_PASSWORD`: **Required**: The password for the `POSTGRES_REPLICATION_USER`. |
| 20 | +* `POSTGRES_REPLICATION_USER`: **Default is `replicator`**: The user that the primary will use to connect |
| 21 | + to the replica. |
| 22 | +* `POSTGRES_REPLICATION_SLOT`: **Default is `replica1`** The replication slot for each replica. |
| 23 | + On the primary, this is a comma-separated list of replication slots. On a replica, this is the name |
| 24 | + of the replication slot used for syncronization. |
| 25 | + |
| 26 | +## Running a Primary server |
| 27 | + |
| 28 | +Example of running a primary instance, with two replication slots. |
| 29 | +You should change the password for the `POSTGRES_PASSWORD` and `POSTGRES_REPLICATION_PASSWORD` |
| 30 | +environment variables in this example: |
| 31 | + |
| 32 | +```bash |
| 33 | +docker volume create postgres-primary |
| 34 | +docker run \ |
| 35 | + --rm --name postgres-primary \ |
| 36 | + -e POSTGRES_REPLICATION_SLOT="replica1,replica2" \ |
| 37 | + -e POSTGRES_REPLICATION_PASSWORD="postgres" \ |
| 38 | + -e POSTGRES_PASSWORD="postgres" \ |
| 39 | + -p 5432:5432 \ |
| 40 | + -v postgres-primary:/var/lib/postgresql/data \ |
| 41 | + ghcr.io/mutablelogic/docker-postgres-darwin-amd64:17-bookworm |
| 42 | +``` |
| 43 | + |
| 44 | +You can add additional replication slots later as needed. |
| 45 | + |
| 46 | +## Running a Replica server |
| 47 | + |
| 48 | +When you run a replica instance, the first time it runs it will backup from the primary instance and then start |
| 49 | +replication. You should change the password for the `POSTGRES_PASSWORD` and `POSTGRES_REPLICATION_PASSWORD` |
| 50 | +environment variables, and set the `POSTGRES_REPLICATION_PRIMARY` environment variable to the primary instance |
| 51 | +in this example: |
| 52 | + |
| 53 | +```bash |
| 54 | +docker volume create postgres-replica1 |
| 55 | +docker run \ |
| 56 | + --rm --name postgres-replica1 \ |
| 57 | + -e POSTGRES_REPLICATION_PRIMARY="host=milou.lan port=5432" \ |
| 58 | + -e POSTGRES_REPLICATION_SLOT="replica1" \ |
| 59 | + -e POSTGRES_REPLICATION_PASSWORD="postgres" \ |
| 60 | + -e POSTGRES_PASSWORD="postgres" \ |
| 61 | + -p 5433:5432 \ |
| 62 | + -v postgres-replica1:/var/lib/postgresql/data \ |
| 63 | + ghcr.io/mutablelogic/docker-postgres-darwin-amd64:17-bookworm |
| 64 | +``` |
| 65 | + |
| 66 | +A second replica (and so forth) can be run in the same way, but with a different port and volume name. |
| 67 | +You can run up to ten replicas by default. |
0 commit comments