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

Learn by Doing - Postgres Logical Replication #281

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

lward27
Copy link
Collaborator

@lward27 lward27 commented Aug 8, 2023

First Draft of "Learn by Doing - Postgresql Logical Replicaiton" - An interactive guide to setting up and exploring Postgresql native logical replicaiton. Enjoy!

Copy link

@snarum00 snarum00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great interactive blog!!

image:
---

Database Replication is the corner stone of highly available and horizontally scaled relational databases. Database replication comes in many different flavors and can be achieved by multiple different types of mechanisms. In order to be better engineers, and architect better systems, it is paramount to have a base understanding of these replication mechanisms. Today, I want to share with you a *Learn by Doing* blog featuring Postgresql's built in Logical Replication.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

built-in

```bash
docker version
```
If your docker environment is configured properly, you should see a bunch of versioning information printed to the terminal window. At the time of this writing, I am running Docker Engine - Community, version 20.10.22.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might add a reminder to have Rancher Desktop open/running. I know thats probably a no brainer but you still see the Version information if you don't have it open but it includes this message 'Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?'

docker run --name postgres-primary -e POSTGRES_PASSWORD=topsecretpassword -d postgres
docker run --name postgres-secondary -e POSTGRES_PASSWORD=topsecretpassword -d postgres
```
To keep things simple, I have used the same password for both databases. This is fine for a learning exercise, but I implore you to make better choices for a production setup.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol

docker inspect postgres-primary
docker inspect postgres-secondary
```
These commands will return a plethora of information regarding your docker containers. We are looking for the value: `Networks.bridge.IPAddress` for both of our containers. Make sure to mark this value down as we will need it later. My values are as follows

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a direct version too docker inspect postgres-primary --format='{{.NetworkSettings.IPAddress}}'

- **postgres-primary** - 172.17.0.2
- **postgres-secondary** - 172.17.0.4

### Connect to the Database using PSQL

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this section and "Run Bash on a container", I would call out that the reader doesn't need to run the commands now but that these are the commands to run later on when prompted. OR move the content into the "Enable Logical Replication" section. It reads as though you should run the psql commands and docker exec before you get to the enable section


# Add a table

Before we setup logical replication, we need to create a database and add a table to both the Primary and Secondary. It's important that the schema's of the two databases match, so I recommend connecting to the primary and running these commands, then immediately connecting to the secondary and running the same commands.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'Before we set up logical replication..." --> "We have one final step before setting up logical replication..."
Repetitive


Before we setup logical replication, we need to create a database and add a table to both the Primary and Secondary. It's important that the schema's of the two databases match, so I recommend connecting to the primary and running these commands, then immediately connecting to the secondary and running the same commands.

Using `psql`, connect to the primary database - `psql -h 172.17.0.2 -p 5432 -U postgres`. Once connected run the following commands:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't use psql to connect without first running docker exec command... that might be an issue with my set up or you might want to add a step for clarity


In this article we created a "test database cluster" using Docker and Postgresql. After launching the primary and secondary database containers, we tweaked the settings on the primary to enable logical replication. Next we verified the connection between the containers worked and then we created a publisher and subscriber relation. After all that setup, we saw logical replication in action with our `INSERT` statement into the primary being replicated to the secondary database.

I hope this "Learn by Doing" article has shed some light on how database clusters operate "under the hood". Obviously, there is a lot more complexity involved when replicating many many database tables with complex relations. If you find yourself in need for a setup like this, or if you need help navigating the intricacies of Postgresql in general, feel free to drop us a line at `sales@ippon.fr`, we are here to help!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'many many' --> 'many'


To remove the two database containers from your system, simply run:
```bash
docker rm postgres-primary

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i had to stop the containers first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants