提问人:Vi. 提问时间:10/11/2023 最后编辑:Vi. 更新时间:10/12/2023 访问量:77
Rails 7 无法从 env 获取数据库密码
Rails 7 can't get database password from env
问:
我有我的 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_PASSWORD
rails 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
答: 暂无答案
评论
ENV["DATABASE_URL"]
password: ENV["DATABASE_PASSWORD"]
"password"=>"ENV[\"DATABASE_PASSWORD\"]"
@