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:


# Creating PGPASS file
echo "$MASTER_HOST:5432:replication:replicator:$REPLICATOR_PASS" >> $PG_PASS_FILE

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

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= --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:


echo "$MASTER_HOST:5432:replication:replicator:$REPLICATOR_PASS" >> $PG_PASS  chmod 0600 $PG_PASS

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.

I’m having the exact same issue. I’ve tried with postgres:14 too without any success.
Did you find anything?

@ pminearods
I finally found the issue.

If you check the script you’ll see that before calling the script we put in /docker-entrypoint-initdb.d/ the ENV VAR PGPASSWORD is set to $POSTGRES_PASSWORD.

When checking the pg_basebackup documentation I found the following statement :
[…]t’s also possible to store the password in the environment variable PGPASSWORD[…]

So I tried to add the following line to my .sh script


And it worked.