Rails 7 无法从 env 获取数据库密码

Rails 7 can't get database password from env

提问人:Vi. 提问时间:10/11/2023 最后编辑:Vi. 更新时间:10/12/2023 访问量:77

问:

我有我的 rails 7.0.4.3 应用程序和通常的 database.yaml conf:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 10
  postgis_schema: public
  schema_search_path: public,postgresql
  host: <%= ENV.fetch("DATABASE_HOST") { "localhost" } %>
  port: <%= ENV.fetch("DATABASE_PORT") { 5432 } %>
  username: <%= ENV.fetch("DATABASE_USERNAME") { %x(whoami).strip } %>
  password: <%= ENV.fetch("DATABASE_PASSWORD") { "password" } %>

development:
  <<: *default
  username: 'postgres'
  password: 'password'
  database: <%= "#{ENV['PROJECT_NAME']}_development" %>

test:
  <<: *default
  username: 'postgres'
  password: 'password'
  database: <%= "#{ENV['PROJECT_NAME']}_test" %>

staging:
  <<: *default
  database: <%= "#{ENV['PROJECT_NAME']}_staging" %>

production:
  <<: *default
  database: <%= "#{ENV['PROJECT_NAME']}_production" %>

dev_remote:
  <<: *default
  database: <%= "#{ENV['PROJECT_NAME']}_dev_remote" %>

应用在远程服务器上的 docker 中运行。 启动时,它给了我这个错误:

rails aborted!
ActiveRecord::ConnectionNotEstablished: connection to server at "REDACTED", port 5438 failed: fe_sendauth: no password supplied


Caused by:
PG::ConnectionBad: connection to server at "REDACTED", port 5438 failed: fe_sendauth: no password supplied

如果我尝试获取环境,则在容器控制台和 .DATABASE_PASSWORDrails c

的输出是这样的:Rails.configuration.database_configuration

=> 
{"default"=>{"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>10, "postgis_schema"=>"public", "schema_search_path"=>"public,postgresql", "host"=>"REDACTED", "port"=>5438, "username"=>"user"},
 "development"=>{"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>10, "postgis_schema"=>"public", "schema_search_path"=>"public,postgresql", "host"=>"REDACTED", "port"=>5438, "username"=>"postgres", "password"=>"password", "database"=>"project_development"},
 "test"=>{"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>10, "postgis_schema"=>"public", "schema_search_path"=>"public,postgresql", "host"=>"REDACTED", "port"=>5438, "username"=>"postgres", "password"=>"password", "database"=>"project_test"},
 "staging"=>{"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>10, "postgis_schema"=>"public", "schema_search_path"=>"public,postgresql", "host"=>"REDACTED", "port"=>5438, "username"=>"user", "database"=>"project_staging"},
 "production"=>{"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>10, "postgis_schema"=>"public", "schema_search_path"=>"public,postgresql", "host"=>"REDACTED", "port"=>5438, "username"=>"user", "database"=>"project_production"},
 
"dev_remote"=>{"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>10, "postgis_schema"=>"public", "schema_search_path"=>"public,postgresql", "host"=>"REDACTED", "port"=>5438, "username"=>"user", "database"=>"project_dev_remote", "password"=>nil}}

"password"=>nil不管我怎么设置。 有没有办法解决这个问题?

Docker文件

FROM ruby:3.1.4-alpine

RUN apk add --update --no-cache bash build-base postgresql-dev tzdata git openssh ssmtp nano

WORKDIR /myapp
ENV RAILS_ENV $RAILS_ENV

COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN gem install bundler -v 2.2.32 && bundle config set --local without 'development test' && bundle install -j $(nproc)

COPY . /myapp

RUN chmod +x /myapp/start-app.sh
CMD /myapp/start-app.sh
Ruby-on-Rails PostgreSQL 环境变量

评论

0赞 max 10/12/2023
你真的需要这个吗?您可以设置和覆盖 YAML 文件中的任何设置。无需额外配置,无需大惊小怪。guides.rubyonrails.org/configuring.html#configuring-a-databaseENV["DATABASE_URL"]
0赞 Benjamin Scharbau 10/12/2023
你能发布你的 Dockerfile 吗?我的猜测是,您的应用程序以与您设置环境变量的用户不同的用户身份运行,但另一方面,您也不应该能够访问其他变量......
0赞 Vi. 10/12/2023
@max如果我在没有 erb 的情况下设置,那么它将被读取为字符串 --> 。@BenjaminScharbau Dockerfile 在操作中。但正如您所说,唯一存在此问题的 env var 是密码。password: ENV["DATABASE_PASSWORD"]"password"=>"ENV[\"DATABASE_PASSWORD\"]"
0赞 Vi. 10/12/2023
@max DATABASE_URL我有同样的问题。DATABASE_PASSWORD为零
1赞 Vi. 10/12/2023
@BenjaminScharbau @max我发现了问题!密码🤬中是一个模棱两可的字符@

答: 暂无答案