|
1 | 1 | # Postgres with LOGIC |
2 | 2 |
|
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 | +--- |
4 | 50 |
|
5 | 51 | <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> |
7 | 53 | </p> |
| 54 | + |
| 55 | +[^1]: Presentation on YouTube: https://www.youtube.com/watch?v=tJegTc-oLtk |
0 commit comments