Skip to content

Commit 21e1f61

Browse files
committed
Add fundamental development configuration
1 parent 494fd53 commit 21e1f61

File tree

13 files changed

+158
-2
lines changed

13 files changed

+158
-2
lines changed

.example.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
POSTGRES_CIDR=172.55.32.0/24
2+
POSTGRES_SUBNET=172.55.0.0/16
3+
POSTGRES_GATEWAY=172.55.32.254

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM postgres:16.2 as base
2+
3+
4+
FROM base as primary
5+
6+
COPY ./postgres/primary/docker-entrypoint-initdb.d/ docker-entrypoint-initdb.d/
7+
8+
9+
FROM base as secondary
10+
11+
COPY ./postgres/secondary/bin/ /usr/local/bin/
12+
ENTRYPOINT [ "docker-entrypoint-override.sh" ]
13+
14+
CMD ["postgres"]
15+
16+
FROM primary

README.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,55 @@
11
# Postgres with LOGIC
22

3-
Production-ready Postgres with Docker that does not suck, with [LOGIC](https://withlogic.co).
3+
This repository contains the configuration for production-grade Postgres with Docker, with [LOGIC](https://withlogic.co). It was first presented on 24 January 2024, at Docker Athens during the presentation [Production grade Postgres with Docker](https://www.youtube.com/watch?v=tJegTc-oLtk)[^1].
4+
5+
The configuration in this repository sets up a primary and secondary Postgres server with streaming replication. This means that the primary server accepts all write queries, which are in turn replicated to the secondary server. Therefore, read queries can be load balanced and performed on both servers.
6+
7+
## Requirements
8+
9+
- Docker Engine 25.0.0 or newer
10+
- Docker Compose 2.24.9 or newer, for development
11+
12+
## Configuration
13+
14+
The setup of Postgres with LOGIC is configured with environment variables and Docker Secrets for sensitive data.
15+
16+
### Environment variables
17+
18+
- `POSTGRES_CIDR`: The CIDR block from which to allocate IPs in the Docker network and also allow replication from (default: `172.54.32.0/24`)
19+
- `POSTGRES_GATEWAY`: The gateway to use in the Docker network (default: `172.54.32.254`)
20+
- `POSTGRES_SUBNET`: The subnet to allocate for the Docker network (default: `172.54.0.0/16`)
21+
22+
For convenience, in development these environment variables can be set in a `.env` environment file. Example file available in [`.example.env`](./.example.env)
23+
24+
## Development
25+
26+
To kick off and evaluate the setup locally, all you have to do is run
27+
28+
```console
29+
docker compose up
30+
```
31+
32+
After all containers start, you can validate the setup with the following steps:
33+
34+
1. Create a table on the primary server
35+
```console
36+
docker compose exec primary psql -U postgres -c "CREATE TABLE people (name varchar(40));"
37+
```
38+
2. Insert a couple of rows in the primary server
39+
```console
40+
docker compose exec primary psql -U postgres -c "INSERT INTO people VALUES ('grace');"
41+
docker compose exec primary psql -U postgres -c "INSERT INTO people VALUES ('alan');"
42+
```
43+
3. Validate that data can be read from both servers
44+
```console
45+
docker compose exec primary psql -U postgres -c "SELECT * FROM people;"
46+
docker compose exec secondary psql -U postgres -c "SELECT * FROM people;"
47+
```
48+
49+
---
450

551
<p align="center">
6-
<img src="https://github.com/withlogicco/postgres/assets/1188592/79797352-66a1-436e-82ac-a6ed33bc5aa8" />
52+
<i>🦄 Built with <a href="https://withlogic.co/">LOGIC</a>. 🦄</i>
753
</p>
54+
55+
[^1]: Presentation on YouTube: https://www.youtube.com/watch?v=tJegTc-oLtk

compose.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
x-base:
2+
&base
3+
secrets:
4+
- postgres-password
5+
- replicator-password
6+
environment:
7+
POSTGRES_PASSWORD_FILE: /run/secrets/postgres-password
8+
POSTGRES_CIDR: ${POSTGRES_CIDR:-172.54.32.0/24}
9+
command: ["postgres", "-c", "log_statement=all"]
10+
11+
services:
12+
primary:
13+
<<: *base
14+
build:
15+
context: .
16+
target: primary
17+
volumes:
18+
- primary_data:/var/lib/postgresql/data
19+
20+
secondary:
21+
<<: *base
22+
build:
23+
context: .
24+
target: secondary
25+
restart: on-failure:3
26+
volumes:
27+
- secondary_data:/var/lib/postgresql/data
28+
29+
secrets:
30+
postgres-password:
31+
file: dev/secrets/postgres-password
32+
replicator-password:
33+
file: dev/secrets/replicator-password
34+
35+
networks:
36+
default:
37+
ipam:
38+
config:
39+
- subnet: ${POSTGRES_SUBNET:-172.54.0.0/16}
40+
ip_range: ${POSTGRES_CIDR:-172.54.32.0/24}
41+
gateway: ${POSTGRES_GATEWAY:-172.54.32.254}
42+
43+
volumes:
44+
primary_data:
45+
secondary_data:

dev/secrets/postgres-password

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
development_password

dev/secrets/replicator-password

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
development_replicator_password
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set -ex
2+
3+
REPLICATOR_PASSWORD=$(cat /run/secrets/replicator-password)
4+
psql -c "CREATE ROLE replicator REPLICATION LOGIN PASSWORD '$REPLICATOR_PASSWORD'"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
set -ex
2+
3+
POSTGRES_CIDR=${POSTGRES_CIDR:-172.54.32.0/24}
4+
PGDATA=${PGDATA:-/var/lib/postgresql/data}
5+
6+
echo "host replication replicator $POSTGRES_CIDR scram-sha-256" >> $PGDATA/pg_hba.conf
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
set -ex
2+
3+
export PGPASSWORD=$(cat /run/secrets/replicator-password)
4+
5+
pg_basebackup -w -h primary -D $PGDATA -U replicator -P -v -X stream
6+
chmod -R 0750 $PGDATA

0 commit comments

Comments
 (0)