【发布时间】:2021-07-18 16:15:54
【问题描述】:
假设我们需要在生产环境中运行第二个 postgres 实例。首先它们 (postgres-one) 已经在运行并且其中几乎没有数据库和数据。现在我想更新我的docker-compose.yaml 文件并为第二个(postgres-two)添加配置。此外,我必须从postgres-one 获取一些数据库信息并将其复制到postgres-two。以下是我试图实现这一目标的方法:
docker-compose.yaml
postgres-two:
image: postgres:12.5
depends_on:
postgres-one:
condition: service_started
...
ports:
- "5433:5432"
command: bash -c "chmod +x /usr/local/bin/init.sh && /usr/local/bin/init.sh"
volumes:
- ./data/postgres-two/init-db/init.sh:/usr/local/bin/init.sh
init.sh
#!/bin/bash
# allows you to skip the password prompt for pg_dump
echo "postgres-one:5432:dbname1_one:dbuser1_one:dbpass1_one" > ~/.pgpass
echo "postgres-one:5432:dbname2_one:dbuser2_one:dbpass2_one" >> ~/.pgpass
chmod 600 ~/.pgpass
# gets the data from external database & copies it to internal
pg_dump -h postgres-one -U dbuser1_one dbname1_one | psql -h localhost -U dbuser1_two -d dbname1_two
pg_dump -h postgres-one -U dbuser2_one dbname2_one| psql -h localhost -U dbuser2_two -d dbname2_two
但是当我运行它时,我得到了错误:
psql:错误:无法连接到服务器:连接被拒绝 服务器是否在主机“localhost”(127.0.0.1)上运行并接受 端口 5432 上的 TCP/IP 连接?
我之前已经尝试过不带-h localhost,它给了我这个我记得的:
psql: error: could not connect to server: No such file or directory 服务器是否在本地运行并接受 Unix 域套接字“/var/run/postgresql/.s.PGSQL.5432”上的连接?
如果我在init.sh 的开头添加find /var/run/postgresql/ -name .s.PGSQL.5432,它将不会显示任何内容。因此,据我了解,我无法继续使用psql,因为 postgres 服务器目前未运行。当然我不能用postgres / pg_ctl命令运行它,因为它们不能被root执行:
不允许“root”执行 PostgreSQL 服务器。
当然,docker 容器默认以 root 用户身份运行,如果我更改用户,它也会给我错误,例如:
chmod:更改“/usr/local/bin/init.sh”的权限:不允许操作
我做错了吗?或者也许我可以得到转储并以其他方式应用它们......不知何故?
【问题讨论】:
标签: postgresql docker docker-compose psql production-environment