<p>From what I have read, I need to make changes to the configuration file <code>postgres.conf</code> and <code>pg_hba.conf</code> and after that we need to create a &quot;replication&quot; user.</p>
<ol>
<li><p>How do I make changes to these configuration files and make sure that the changes to these configuration files persist through the restarts?</p>
</li>
<li><p>Since my postgres is running inside a kubernetes pod, I reckon that the user must be created inside the container.
But when I do <code>createuser -replication -P replica</code>, it wont allow me to create one since the current user after I did <code>docker exec -it &lt;container_id&gt; bash</code> is <code>root</code> and not <code>postgres</code>.</p>
</li>
</ol>

<p>This <a href="https://gist.github.com/avshabanov/eb8e03a050c79f8e77420b06f9b4abe5" rel="nofollow noreferrer">gist</a> has a detailed explanation on how to setup a master-slave replication with Postgres using docker.</p>
<h2>Master configuration</h2>
<h3>Create replication_user</h3>
<pre><code>CREATE USER replication_user NOSUPERUSER;
ALTER USER replication_user WITH REPLICATION;
ALTER USER replication_user WITH PASSWORD 'CHANGEME';
</code></pre>
<h3>postgresql.conf</h3>
<p>Edit or add these lines:</p>
<pre><code>listen_addresses = '*'   # It can be more specific with the listen IP
wal_level = hot_standby  
max_wal_sender = 5       #  Specifies the maximum number of concurrent connections from standby servers
ssl = off
archive_mode = on   
archive_command = &quot;cp %p /path/to/archive/%f&quot;
</code></pre>
<h3>pg_hba.conf</h3>
<p>Let's suppose that 192.168.0.2/32 is the IP of the slave and replication_user is the replication user.</p>
<p><strong>case no authentication is required</strong></p>
<pre><code> host  replication   replication_user   192.168.0.2/32      trust
</code></pre>
<p><strong>case authentication using password</strong></p>
<pre><code> host  replication   replication_user   192.168.0.2/32      scram-sha-256 # or MD5
</code></pre>
<h2>Slave configuration</h2>
<p>Let's suppose that the Master IP is: 192.168.0.1 and the port is the default (5432).</p>
<pre><code>listen_addresses = '*'
primary_conninfo = 'user=replication_user password=CHANGEME host=192.168.0.1 port=5432'
hot_standby = on
</code></pre>
<h2>Making the configuration persistent</h2>
<p>You can use <a href="https://docs.docker.com/storage/volumes/" rel="nofollow noreferrer">docker volumes</a> (or <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/" rel="nofollow noreferrer">for kubernetes</a>).
Start by creating the configuration files in the local machine that start the containers and then:</p>
<pre><code>docker run --rm --name IMAGE_NAME -v /local/path/to/postgresql.conf:/etc/postgresql/postgresql.conf -v /local/path/to/pg_hba.conf:/var/lib/postgresql/data/pg_hba.conf CONTAINER_NAME
</code></pre>
<p>The same for the slaves.</p>
<h1>sources:</h1>
<p><a href="https://wiki.postgresql.org/wiki/Streaming_Replication" rel="nofollow noreferrer">https://wiki.postgresql.org/wiki/Streaming_Replication</a></p>
<p><a href="https://gist.github.com/avshabanov/eb8e03a050c79f8e77420b06f9b4abe5" rel="nofollow noreferrer">https://gist.github.com/avshabanov/eb8e03a050c79f8e77420b06f9b4abe5</a></p>
