Master-Slave configuration for postgresql in docker

details:
OS: Debian 9
Docker Version: 18.09.0
I installed bitnami-postgresql:latest docker images in two independent servers. Configured everything as the bitnami documentation. Ran the docker compose file. You can find the config of the docker compose file below.
When the master(1) is down, the slave(2) works as master after running the command
docker exec postgresql-slave touch /tmp/postgresql.trigger.5432
Question:
My question is when the master(1) server is up how does the master(1) server know that it should work as a slave or act as master (turning the master(previous slave) to slave)?
Because if both of the servers act as master at the same time, various conflicts can occur.
Or should the old master be setup completely as a new slave from scratch?(If so, It is not feasible for larger databases)

Another question, since the environment variable POSTGRESQL_REPLICATION_MODE=slave for the original slave container, what will happen to it if the container restarts after it becomes the master?

Docker Compose

version: '2'

services:
    postgresq:
        image: 'bitnami/postgresql:latest'
        ports: 
                - 'port:port'
        environment:
                - POSTGRESQL_USERNAME=user
                - POSTGRESQL_PASSWORD= **********
                - POSTGRESQL_DATABASE=user_db
                - POSTGRESQL_REPLICATION_MODE=master/slave(for slave)
                - POSTGRESQL_MASTER_HOST='ip'
                - POSTGRESQL_MASTER_PORT_NUMBER='port'
                - POSTGRESQL_REPLICATION_USER=repl_user
                - POSTGRESQL_REPLICATION_PASSWORD= **********

Hi @mohan,

Thank you for using Bitnami. Our team is the information you provided and will update this thread as soon as possible.

Hi @mohan,
I will reply between your lines:

My question is when the master(1) server is up how does the master(1) server know that it should work as a slave or act as master (turning the master(previous slave) to slave)?

Master/Slave role is set through the POSTGRESQL_REPLICATION_MODE environment variable. This will affect the parameters with which it is started.

Another question, since the environment variable POSTGRESQL_REPLICATION_MODE=slave for the original slave container, what will happen to it if the container restarts after it becomes the master?

This node role is set through that environment variable, so it will not become master. Have into account that postgresql master-slave is just for replication it is not a cluster where nodes can change role dynamically.

Maybe you are interested in checking the postgresql documentation about replication.

Best Regards
Rafael Rios

hello @rafaelrios,
Thank you for your answer.
Would it be better if I shut both of the containers down and start the previous slave container with the ENV set to master. Also I am still not clear about this.
Should the old master be setup completely as a new slave from scratch?(If so, It is not feasible for larger databases)
I am checking on the documentation link that you have provided.
Thank you in advance.

Hi moham,
I think, if you lose the master and you reconfigure a slave to act as master it would be safe. In any case, you should make a backup before trying anything that can be risky for your data.

Should the old master be setup completely as a new slave from scratch?(If so, It is not feasible for larger databases)

I am not sure what are you asking me here, I mean, Are you expecting that if you don’t start from scratch it will only need to sync the missing data ? In that case, I can’t what will happen.
For this kind of cases I would refer you to the postgresql’s official documentation or postgresql’s official forums, where specialized people can answer this with a good knowledge of the in and outs of replication and how to recover from a failure situation.

Best regards,
Rafael Rios

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.