Docker Community Forums

Share and learn in the Docker community.

Docker Postgres 13 Image unable to execute pg_basebackup during init

I am creating a container by extending the Postgres image in order to have a standby server receiving streaming replication. I create a script that goes into the ‘/docker-entrypoint-initdb.d/’ to be executed during database initialization. I have the following commands in my bash shell script:

MASTER_HOST=172.17.0.2

# Creating PGPASS file
PG_PASS_FILE=~/.pgpass
echo "$MASTER_HOST:5432:replication:replicator:$REPLICATOR_PASS" >> $PG_PASS_FILE

# Setting proper permissions according to Postgres documentation
chmod 0600 $PG_PASS_FILE

# Executing PG_BASEBACKUP
pg_basebackup --host=$MASTER_HOST --port=5432 --username=replicator --no-password --pgdata=/tmp/basebackup --slot=replication_slot_name --wal-method=stream --format=plain --write-recovery-conf --progress
 

When executing this part of the script, I get the following error:

pg_basebackup: could not connect to server: FATAL: password authentication failed for user “replicator”

Debugging the problem, I decided to try to run pg_basebackup from ‘docker exec’ after the container was created:

docker exec --user=postgres postgres_slave_container pg_basebackup --host=172.17.0.2 --port=5432 --username=replicator --no-password --pgdata=/tmp/basebackup --slot=replication_slot_name --wal-method=stream --format=plain --write-recovery-conf --progress

This executed successfully.

To do a little more debugging, I decided to remake the container and have the init.db script create an executable to run the pg_basebackup after the container was created:

MASTER_HOST=172.17.0.2

PG_PASS=~/.pgpass
echo "$MASTER_HOST:5432:replication:replicator:$REPLICATOR_PASS" >> $PG_PASS  chmod 0600 $PG_PASS
DS_BASEBACKUP=/tmp/ds_basebackup

echo "#!/bin/bash" >> $DS_BASEBACKUP
echo "" >> $DS_BASEBACKUP
echo "pg_basebackup --host=$MASTER_HOST --port=5432 --username=replicator --no-password --pgdata=/tmp/basebackup --slot=replication_slot_name --wal-method=stream --format=plain --write-recovery-conf --progress" >> $DS_BASEBACKUP
echo "" >> $DS_BASEBACKUP

chmod 755 $DS_BASEBACKUP

Executing that script on the command line of the host machine worked as well:

docker exec --user=postgres ds_postgres_slave_container ls /tmp/basebackup

How is it that the exact same pg_basebackup script executed in the same environment with the same user can connect to the Master Host after the container has been created, but not while Postgres is being created?

Is there something with pg_basebackup, that needs Postgres up and running with all the configs before it will execute properly? From the documentation I have read, this does not seem to be the case; but I am at a loss as to why I am getting the error.