没有访问`database.yml`文件中的环境变量 [英] Failing to access environment variables within `database.yml` file

查看:139
本文介绍了没有访问`database.yml`文件中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 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打开rails控制台时,exec rails console 并键入 ENV ['APP_USER_POSTGRES_PASSWORD'] 我收回我在本地配置文件中指定的DB密码。但是,当我启动我的rails服务器时,它无法连接到DB,失败与

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.

有没有什么特别的关于我缺少的语法,或是由于某些原因在$ code> 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆