Rails/PostgreSQL - PG::ConnectionBad: FATAL: 用户密码验证失败(对于 CI/CD)

发布于 2025-01-10 04:08:28 字数 2723 浏览 0 评论 0原文

首先,我知道这个问题有答案:

  1. PG:: ConnectionBad:致命:用户“alphauser”的密码身份验证失败
  2. Rails:致命 - 用户对等身份验证失败(PG::Error)

但在每种情况下,您都必须手动执行此操作,我想找到一个可以在 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:

  1. PG::ConnectionBad: FATAL: password authentication failed for user "alphauser"
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花辞树 2025-01-17 04:08:28

通过从生产中删除用户名密码并保留这些值的默认值来解决这个问题:

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

Solved it by removing the username and password from the production and keeping those values from the default one:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文