Kurt Peek
Kurt Peek

Reputation: 57691

"psql: fe_sendauth: no password supplied" when running postgres as a Docker service

I'm trying to run the following a Docker Compose multi-container application with the following docker-compose.yml:

version: '3'

services:
  postgres:
    image: postgres
    environment:
      - POSTGRES_USER=iperuser
      - POSTGRES_PASSWORD=iperpassword
      - PGDATA=/var/lib/postgresql/data/apks
      - POSTGRES_DB=iper_apks

  alchemy:
    build: ./alchemy
    environment:
      - PGHOST=postgres
      - PGPORT=5432
      - PGUSER=iperuser
    links:
      - postgres

I would like to the alchemy service to wait for postgres to be ready to accept commands using the wait-for-postgres.sh script at https://docs.docker.com/compose/startup-order/:

#!/bin/bash
# wait-for-postgres.sh
# From https://docs.docker.com/compose/startup-order/

set -e

host="$1"
shift
cmd="$@"

until psql -h "$host" -U "postgres" -c '\l'; do
  >&2 echo "Postgres is unavailable - sleeping"
  sleep 1
done

>&2 echo "Postgres is up - executing command"
exec $cmd

The Dockerfile for the alchemy service is

FROM python
RUN apt-get update && apt-get install --assume-yes postgresql
RUN pip install sqlalchemy psycopg2
COPY . /alchemy
WORKDIR alchemy
RUN chmod +x wait-for-postgres.sh
CMD ["./wait-for-postgres.sh", "postgres", "python", "apk_tables.py"]

where I'm using the hostname postgres, since this is the name of the service corresponding to the postgres Docker image. The problem is that if I docker-compose build followed by docker-compose up, I get the following logs:

Starting apkapi_postgres_1
Recreating apkapi_alchemy_1
Attaching to apkapi_postgres_1, apkapi_alchemy_1
postgres_1  | LOG:  database system was shut down at 2017-06-26 17:22:32 UTC
alchemy_1   | Password for user postgres: 
postgres_1  | LOG:  MultiXact member wraparound protections are now enabled
alchemy_1   | psql: fe_sendauth: no password supplied
postgres_1  | LOG:  database system is ready to accept connections
alchemy_1   | Postgres is unavailable - sleeping
postgres_1  | LOG:  autovacuum launcher started
alchemy_1   | Password for user postgres: 
alchemy_1   | psql: fe_sendauth: no password supplied
alchemy_1   | Postgres is unavailable - sleeping
alchemy_1   | Password for user postgres: 
alchemy_1   | psql: fe_sendauth: no password supplied
alchemy_1   | Postgres is unavailable - sleeping

which keeps going on forever.

What I suspect is that the wait-for-postgres.sh assumes that PostgreSQL is running on the local host, which is trusted, whereas a database running on a different host requires a password. Is this the case? If so, how can I modify the script to make it use a password (which in this case I suppose would be iperpassword)?

Upvotes: 5

Views: 7386

Answers (1)

programmerq
programmerq

Reputation: 6554

Looks like you simply need to pass the password into the psql command in order for it to be able to present the password that your remote host requires. The following question gives the options for achieving that: Postgresql: Scripting psql execution with password

Upvotes: 2

Related Questions