Rails/PostgreSQL - PG::ConnectionBad: FATAL: 用户密码验证失败(对于 CI/CD)
首先,我知道这个问题有答案:
但在每种情况下,您都必须手动执行此操作,我想找到一个可以在 CI/CD 管道上自动执行以在生产中部署的答案。
我收到的错误是这样的:
FATAL: password authentication failed for user "Jenkins_project"
Couldn't create 'Jenkins_project_production' database. Please check your configuration.
rake aborted!
PG::ConnectionBad: FATAL: password authentication failed for user "Jenkins_project"
当我尝试创建和迁移数据库时会发生这种情况:
docker-compose -f docker-compose.override.yml --env-file .env run web rake db:create db:migrate
我使用 -f docker-compose.override.yml
因为我试图将其部署在 AWS EC2 服务器中。 docker-compose.override.yml
看起来像这样:
version: "3"
services:
db:
image: postgres
environment:
POSTGRES_USER: ${POSTGRES_USER:-default}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-default}
JENKINS_PROJECT_DATABASE_PASSWORD: ${JENKINS_PROJECT_DATABASE_PASSWORD:-default}
volumes:
- ./tmp/db:/var/lib/postgresql/data
web:
build: .
ports:
- "3000:3000"
depends_on:
- db
environment:
POSTGRES_USER: ${POSTGRES_USER:-default}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-default}
POSTGRES_HOST: ${POSTGRES_HOST:-default}
JENKINS_PROJECT_DATABASE_PASSWORD: ${JENKINS_PROJECT_DATABASE_PASSWORD:-default}
这连接到 .env
文件,所以这就是我调用 --env-file 的原因。 env
在 docker-compose 命令上。
.env
看起来像这样:
POSTGRES_USER=user
POSTGRES_PASSWORD=password
POSTGRES_HOST=host
JENKINS_PROJECT_DATABASE_PASSWORD=jenkins_password
我的 database.yml
看起来像这样:
default: &default
adapter: postgresql
encoding: unicode
host: db
username: user
password: password
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: Jenkins_project_development
production:
<<: *default
database: Jenkins_project_production
username: Jenkins_project
password: <%= ENV['JENKINS_PROJECT_DATABASE_PASSWORD'] %>
我想做的是传递 .env
文件“secrets”运行命令时添加到 docker-compose.override.yml
上的每个服务,并从那里填写 <%= ENV['JENKINS_PROJECT_DATABASE_PASSWORD'] %>
在database.yml
上。
看来我做错了什么,但我不知道是什么。 同样,我知道我可以手动执行此操作,但我想让 CI/CD 管道自动部署。
如果有人有任何想法请分享。谢谢!
First of all, I know there are answers to this question:
- PG::ConnectionBad: FATAL: password authentication failed for user "alphauser"
- Rails: FATAL - Peer authentication failed for user (PG::Error)
But in every case, you have to do it manually and I would like to find an answer that I can do automatically on my CI/CD pipeline to deploy in production.
The error I get is this:
FATAL: password authentication failed for user "Jenkins_project"
Couldn't create 'Jenkins_project_production' database. Please check your configuration.
rake aborted!
PG::ConnectionBad: FATAL: password authentication failed for user "Jenkins_project"
This happens when I try to create and migrate the DB:
docker-compose -f docker-compose.override.yml --env-file .env run web rake db:create db:migrate
I use -f docker-compose.override.yml
because I am trying to deploy it in a AWS EC2 server. The docker-compose.override.yml
looks like this:
version: "3"
services:
db:
image: postgres
environment:
POSTGRES_USER: ${POSTGRES_USER:-default}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-default}
JENKINS_PROJECT_DATABASE_PASSWORD: ${JENKINS_PROJECT_DATABASE_PASSWORD:-default}
volumes:
- ./tmp/db:/var/lib/postgresql/data
web:
build: .
ports:
- "3000:3000"
depends_on:
- db
environment:
POSTGRES_USER: ${POSTGRES_USER:-default}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-default}
POSTGRES_HOST: ${POSTGRES_HOST:-default}
JENKINS_PROJECT_DATABASE_PASSWORD: ${JENKINS_PROJECT_DATABASE_PASSWORD:-default}
And this is connected to the .env
file, so that's why I call --env-file .env
on the docker-compose
command.
The .env
looks like this:
POSTGRES_USER=user
POSTGRES_PASSWORD=password
POSTGRES_HOST=host
JENKINS_PROJECT_DATABASE_PASSWORD=jenkins_password
My database.yml
looks like this:
default: &default
adapter: postgresql
encoding: unicode
host: db
username: user
password: password
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: Jenkins_project_development
production:
<<: *default
database: Jenkins_project_production
username: Jenkins_project
password: <%= ENV['JENKINS_PROJECT_DATABASE_PASSWORD'] %>
What I am trying to do is pass the .env
file "secrets" to each service on the docker-compose.override.yml
when running the command and from there fill the <%= ENV['JENKINS_PROJECT_DATABASE_PASSWORD'] %>
on the database.yml
.
It seems I am doing something wrong but I can't figure out what.
Again, I know I can do it manually but I want to make it automatic for the CI/CD pipeline to deploy.
If anyone has any idea please share. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过从生产中删除
用户名
和密码
并保留这些值的默认值来解决这个问题:Solved it by removing the
username
andpassword
from the production and keeping those values from the default one: