python - 将 postgres 作为 Docker 服务运行时为 "psql: fe_sendauth: no password supplied"

标签 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 准备好接受使用 wait-for-postgres.sh 的命令脚本位于 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

alchemy 服务的 Dockerfile

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)?

最佳答案

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

关于python - 将 postgres 作为 Docker 服务运行时为 "psql: fe_sendauth: no password supplied",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44765574/

相关文章:

sql - 计算所有记录中两个日期时间之间的平均时间?

python - 如何舍入 Pandas 数据框中的日期时间索引?

python - 使用 python 在 Azure 中部署资源

python - 仅在 UNIX 系统上读取文件时出现 UnicodeDecodeError

bash - curl:是否可以在shell中全局设置超时

linux - 使用awk计算平均成绩

Bash:在许多文件上并行化 md5sum 校验和

ruby-on-rails - 英雄数据库 :pull Failed to connect to database NoMethodError

python - 我如何读取文件夹和子文件夹 *.wav ;和训练模型输入的特征提取?

postgresql - 在Azure容器应用程序中部署Postgres DB