无法访问“database.yml"文件中的环境变量 [英] Failing to access environment variables within `database.yml` file
问题描述
我的 development.yml
文件有以下开发部分:
I have the following developement section of my development.yml
file:
development:
adapter: postgresql
host: localhost
database: testtb
username: app_user
password: ENV['APP_USER_POSTGRES_PASSWORD'] <= Troublesome line
当我通过 bundle exec rails console
打开 Rails 控制台并输入 ENV['APP_USER_POSTGRES_PASSWORD']
时,我会取回我在本地配置文件中指定的数据库密码.但是,当我启动我的 rails 服务器时,它无法连接到数据库,失败并显示
When I open a rails console via bundle exec rails console
and type ENV['APP_USER_POSTGRES_PASSWORD']
I get back the DB password I've specified in my local profile. However, when I start my rails server, it can't connect to the DB, failing with
PGError FATAL: password authentication failed for user "app_user"
当我实际上以纯文本形式输入数据库密码而不是尝试通过 ENV['...']
访问它时,这以前是有效的,但出于显而易见的原因,我想将实际密码完全保留在此文件之外(因此保留在代码存储库之外),同时仍然能够向 database.yml
文件提交其他非安全更改.
This was previously working when I had the DB password actually typed out in plain text, rather than trying to access it via ENV['...']
, but for obvious reasons I want to keep the actual password out of this file entirely (and therefore out of the code repository) while still being able to commit other, non-secure changes to the database.yml
file.
我缺少的语法有什么特别之处,还是在加载 database.yml
文件时环境变量由于某种原因不可用?
Is there something special about the syntax I'm missing, or are the environment variables for some reason not available when the database.yml
file is being loaded?
推荐答案
更新: 有人在评论中报告说这个 从 Rails 4.2.xx 起不起作用我自己没试过,所以YMMV.
Update: Some people report in the comments that this doesn't work as of Rails 4.2.x.x. I haven't tried it myself, so YMMV.
啊,终于找到了简单的解决方案——它接受嵌入式 Ruby:
Ah, finally figured out the simple solution - it accepts embedded Ruby:
password: <%= ENV['APP_USER_POSTGRES_PASSWORD'] %>
这篇关于无法访问“database.yml"文件中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!