.pgpass在Dockerized环境中用于PostgreSQL复制
我尝试使用Docker和bash脚本(我使用Coreos)来设置PostgreSQL从站。 我还没有find任何方法来提供有效的.pgpass
。
我知道我可以创build一个PGPASSWORD环境variables,但不希望出于安全原因这样做(如http://www.postgresql.org/docs/current/static/libpq-envars.html所述 ),因为每次使用recovery.conf文件(对于primary_conninfovariables)都应该可以访问该密码。
Dockerfile
# ... # apt-get installs and other config # ... USER postgres # Create role and db RUN /etc/init.d/postgresql start &&\ psql --command "CREATE USER replicator WITH ENCRYPTED PASSWORD 'THEPASSWORD';" &&\ psql --command "CREATE DATABASE db WITH OWNER replicator;" # Set the pg_pass to allow connection to master ADD ./pgpass.conf /home/postgres/.pgpass # pgpass.conf comes my root git folder USER root RUN chmod 0600 /home/postgres/.pgpass
在我的bash文件中
# ... pg_basebackup -h host.of.master.ip -D /var/pgbackup/backup_data -U replicator -v -P # ...
问题似乎是pgpass文件没有被读取。 看来我应该使用我正在sudoing的用户的密码( https://serverfault.com/questions/526170/psql-fe-sendauth-no-password-supplied ),但在这种情况下,复制器的作用是自然的不是一个可用的bash用户。 (请注意,将pgpass复制到/ home / root不是/ home / postgres不起作用)。
注意:我的pgpass文件和远程数据库conf
# pgpass.conf host.of.master.ip:5432:replication:replicator:THEPASSWORD host.of.master.ip:5432:*:replicator:THEPASSWORD # pg_hba.conf host replication replicator host.of.slave.ip/24 md5
你必须在将要运行命令的用户的home文件夹(在本例中为postgres
)上创build一个.pgpass
文件。 文件的每一行必须采用hostname:port:database:username:password
格式,并且支持通配符,因此您可以将数据库设置为“*”。
就我而言,我有这样的事情…
$ sudo echo "${PRIMARY_IP}:5432:*:${REPL_USER}:${REPL_PASS}" > /var/lib/postgresql/.pgpass $ sudo chown postgres:postgres /var/lib/postgresql/.pgpass $ sudo chmod 0600 /var/lib/postgresql/.pgpass $ sudo -u postgres pg_basebackup -h $PRIMARY_IP -D /var/lib/postgresql/9.4/main -U ${REPL_USER} -v -P --xlog-method=stream
这些variables(例如PRIMARY_IP)是在我用-e PRIMARY_IP=xxxx
运行docker容器时设置的