Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibily for PG 12.3 #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions 10-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ echo "hot_standby = on" >> "$PGDATA/postgresql.conf"


echo "host replication $REPLICATION_USER 0.0.0.0/0 trust" >> "$PGDATA/pg_hba.conf"

pg_ctl -D "$PGDATA" -m fast -w reload
4 changes: 2 additions & 2 deletions 20-replication.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -e

if [ $REPLICATION_ROLE = "master" ]; then
psql -U postgres -c "CREATE ROLE $REPLICATION_USER WITH REPLICATION PASSWORD '$REPLICATION_PASSWORD' LOGIN"
psql -U $POSTGRES_USER -c "CREATE ROLE $REPLICATION_USER WITH REPLICATION PASSWORD '$REPLICATION_PASSWORD' LOGIN"

elif [ $REPLICATION_ROLE = "slave" ]; then
# stop postgres instance and reset PGDATA,
Expand All @@ -14,7 +14,7 @@ elif [ $REPLICATION_ROLE = "slave" ]; then
pg_basebackup \
--write-recovery-conf \
--pgdata="$PGDATA" \
--xlog-method=fetch \
--wal-method=fetch \
--username=$REPLICATION_USER \
--host=$POSTGRES_MASTER_SERVICE_HOST \
--port=$POSTGRES_MASTER_SERVICE_PORT \
Expand Down
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- mode: conf -*-
FROM postgres:9.6
FROM postgres:12.3

MAINTAINER [email protected]

Expand All @@ -17,6 +17,9 @@ ENV REPLICATION_PASSWORD ""
ENV POSTGRES_MASTER_SERVICE_HOST localhost
ENV POSTGRES_MASTER_SERVICE_PORT 5432

# postgres settings
ENV POSTGRES_USER postgres

COPY 10-config.sh /docker-entrypoint-initdb.d/
COPY 20-replication.sh /docker-entrypoint-initdb.d/
# Evaluate vars inside PGDATA at runtime.
Expand Down
95 changes: 70 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,95 @@
Postgres Streaming Replication
==============================
# Postgres Streaming Replication

[![Build Status](https://travis-ci.org/nebirhos/docker-postgres-replication.svg?branch=master)](https://travis-ci.org/nebirhos/docker-postgres-replication)
[![](https://imagelayers.io/badge/nebirhos/postgres-replication:latest.svg)](https://imagelayers.io/?images=nebirhos/postgres-replication:latest 'Get your own badge on imagelayers.io')

[![](https://imagelayers.io/badge/nebirhos/postgres-replication:latest.svg)](https://imagelayers.io/?images=nebirhos/postgres-replication:latest "Get your own badge on imagelayers.io")

Enhanced version of the official Postgres image to support streaming replication
out of the box.

Postgres-replication is meant to be used with orchestration systems such as Kubernetes.

## Tags

Tags
----

Images are automatically updated when the [official postgres image](https://hub.docker.com/_/postgres/)
is updated.
Images are automatically updated when the [official postgres image](https://hub.docker.com/_/postgres/) is updated.

Supported tags:

* 9.6, 9, latest ([9.6](https://github.com/nebirhos/docker-postgres-replication/tree/9.6))
* 9.5 ([9.5](https://github.com/nebirhos/docker-postgres-replication/tree/9.5))
- 9.6, 9, latest ([9.6](https://github.com/nebirhos/docker-postgres-replication/tree/9.6))
- 9.5 ([9.5](https://github.com/nebirhos/docker-postgres-replication/tree/9.5))
- 12.3 ([12.3](https://github.com/nebirhos/docker-postgres-replication/tree/12.3))

## Run with Docker Compose

Run with Docker Compose
-----------------------
```sh
docker-compose up
```

Then you can try to make somes changes on master

```sh
docker exec -it docker-postgres-replication_postgres-master_1 psql -U postgres
```
docker-compose up

```sql
postgres=# create database test;
postgres=# \c test
test=# create table posts (title text);
test=# insert into posts values ('it works');
```

Then changes will appear on slave

Run with Docker
---------------
```sh
docker exec docker-postgres-replication_postgres-slave_1 psql -U postgres test -c 'select * from posts'
title
----------
it works
(1 row)
```

### Exemple of `docker-compose.yml`

```yml
version: "3.3"
services:
postgres:
image: nebirhos/postgres-replication:12.3
shm_size: "2gb"
environment:
POSTGRES_USER: nebirhos
POSTGRES_PASSWORD: password
REPLICATION_USER: nebirhos_rep
REPLICATION_PASSWORD: password
ports:
- 5432:5432
expose:
- 5432

postgres-slave:
image: nebirhos/postgres-replication:12.3
links:
- postgres
environment:
POSTGRES_USER: nebirhos
POSTGRES_PASSWORD: password
REPLICATION_USER: nebirhos_rep
REPLICATION_PASSWORD: password
REPLICATION_ROLE: slave
POSTGRES_MASTER_SERVICE_HOST: postgres
expose:
- 5432
depends_on:
- postgres
```

## Run with Docker

To run with Docker, first run the Postgres master:

```
```sh
docker run -p 127.0.0.1:5432:5432 --name postgres-master nebirhos/postgres-replication
```


Then Postgres slave(s):

```
Expand All @@ -50,12 +99,8 @@ docker run -p 127.0.0.1:5433:5432 --link postgres-master \
-t nebirhos/postgres-replication
```

## Notes

Notes
-----

Replication is set up at container start by putting scripts in the `/docker-entrypoint-initdb.d` folder.
This way the original Postgres image scripts are left untouched.
Replication is set up at container start by putting scripts in the `/docker-entrypoint-initdb.d` folder. This way the original Postgres image scripts are left untouched.

See [Dockerfile](Dockerfile) and [official Postgres image](https://hub.docker.com/_/postgres/)
for custom environment variables.
See [Dockerfile](Dockerfile) and [official Postgres image](https://hub.docker.com/_/postgres/) for custom environment variables.