如何在 .env 中为 docker-compose 生成随机 PostgreSQL 密码
使用以下命令生成密码:
generate_pg_password.sh
echo "POSTGRES_PASSWORD=$(pwgen 30)" >> .env然后在 docker-compose.yml 中,你可以这样使用环境变量:
docker-compose.yml
services:
postgres:
image: postgres:latest
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
volumes:
- ./postgres_data:/var/lib/postgresql/data
restart: unless-stopped
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 1s
start_period: 3s
timeout: 5s
retries: 30If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow