" psql:fe_sendauth:没有提供密码"当运行postgres作为Docker服务时

时间:2017-06-26 17:46:04

标签: python bash postgresql docker

我尝试使用以下docker-compose.yml运行以下Docker Compose多容器应用程序:

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

我希望alchemy服务等待postgres准备好使用https://docs.docker.com/compose/startup-order/上的wait-for-postgres.sh脚本接受命令:

#!/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

Dockerfile服务的alchemy

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"]

我使用主机名postgres,因为这是与postgres Docker镜像对应的服务的名称。问题是,如果我docker-compose build后跟docker-compose up,我会收到以下日志:

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

永远持续下去。

我怀疑wait-for-postgres.sh假设PostgreSQL在本地主机上运行,​​该主机是受信任的,而在不同主机上运行的数据库需要密码。是这样的吗?如果是这样,我如何修改脚本以使其使用密码(在这种情况下,我认为是iperpassword)?

1 个答案:

答案 0 :(得分:1)

看起来您只需将密码传递到psql命令,以便它能够显示远程主机所需的密码。以下问题提供了实现这一目标的选项:Postgresql: Scripting psql execution with password