I have the following developement section of my development.yml
file:
我的发展有以下发展阶段。yml文件:
development:
adapter: postgresql
host: localhost
database: testtb
username: app_user
password: ENV['APP_USER_POSTGRES_PASSWORD'] <= Troublesome line
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
当我通过bundle exec rails控制台打开rails控制台并输入ENV['APP_USER_POSTGRES_PASSWORD']时,我将返回我在本地配置文件中指定的DB密码。然而,当我启动rails服务器时,它无法连接到DB,失败了
PGError FATAL: password authentication failed for user "app_user"
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.
当我用纯文本输入DB密码而不是试图通过ENV['…,但出于明显的原因,我希望将实际的密码完全从这个文件中删除(因此也不包括代码存储库),同时仍然能够提交对数据库的其他非安全更改。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?
对于我所缺少的语法有什么特别的地方吗?或者是由于某些原因导致的环境变量在数据库中不可用。正在加载yml文件?
2 个解决方案
#1
162
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.
更新:有些人在评论中说,这在Rails 4.2.x中是行不通的。我自己还没试过,YMMV。
Ah, finally figured out the simple solution - it accepts embedded Ruby:
啊,终于想出了一个简单的解决方案——它接受嵌入的Ruby:
password: <%= ENV['APP_USER_POSTGRES_PASSWORD'] %>
#2
18
Short and quick solution if you are running a newer Rails version! Run the following command:
如果您正在运行一个更新的Rails版本,那么这将是一个简短而快速的解决方案!运行以下命令:
spring stop
..then run rails console
or other rails command. My issue was that Spring server needed to be restarted in order to refresh/pickup my new ENV vars. I was starting up Rails console and it couldn't see them until I shut down Spring.
. .然后运行rails控制台或其他rails命令。我的问题是Spring服务器需要重新启动以刷新/获取我的新ENV vars。我正在启动Rails控制台,直到我关闭Spring,它才能看到它们。
Previous versions of Rails didn't have this issue since they didn't use Spring server.
以前的Rails版本没有这个问题,因为它们没有使用Spring服务器。
Another tool to help you troubleshoot -- Use the following command to print out your database.yml config. You can run it from the command line, but I prefer to run this within Rails console since then you can use awesome_print to make it pretty:
另一个帮助您进行故障排除的工具——使用以下命令打印数据库。yml配置。您可以从命令行运行它,但是我更喜欢在Rails控制台中运行它,因为这样您就可以使用awesome_print来使它更美观:
Within rails console
:
在rails控制台:
puts ActiveRecord::Base.configurations
...or using awesome_print
…或者使用awesome_print
ap ActiveRecord::Base.configurations
Or instead from the command line:
或者从命令行:
bin/rails runner 'puts ActiveRecord::Base.configurations'
#1
162
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.
更新:有些人在评论中说,这在Rails 4.2.x中是行不通的。我自己还没试过,YMMV。
Ah, finally figured out the simple solution - it accepts embedded Ruby:
啊,终于想出了一个简单的解决方案——它接受嵌入的Ruby:
password: <%= ENV['APP_USER_POSTGRES_PASSWORD'] %>
#2
18
Short and quick solution if you are running a newer Rails version! Run the following command:
如果您正在运行一个更新的Rails版本,那么这将是一个简短而快速的解决方案!运行以下命令:
spring stop
..then run rails console
or other rails command. My issue was that Spring server needed to be restarted in order to refresh/pickup my new ENV vars. I was starting up Rails console and it couldn't see them until I shut down Spring.
. .然后运行rails控制台或其他rails命令。我的问题是Spring服务器需要重新启动以刷新/获取我的新ENV vars。我正在启动Rails控制台,直到我关闭Spring,它才能看到它们。
Previous versions of Rails didn't have this issue since they didn't use Spring server.
以前的Rails版本没有这个问题,因为它们没有使用Spring服务器。
Another tool to help you troubleshoot -- Use the following command to print out your database.yml config. You can run it from the command line, but I prefer to run this within Rails console since then you can use awesome_print to make it pretty:
另一个帮助您进行故障排除的工具——使用以下命令打印数据库。yml配置。您可以从命令行运行它,但是我更喜欢在Rails控制台中运行它,因为这样您就可以使用awesome_print来使它更美观:
Within rails console
:
在rails控制台:
puts ActiveRecord::Base.configurations
...or using awesome_print
…或者使用awesome_print
ap ActiveRecord::Base.configurations
Or instead from the command line:
或者从命令行:
bin/rails runner 'puts ActiveRecord::Base.configurations'