Rails + PostgreSQL + rbenv-vars: fe_sendauth: 没有使用环境变量提供密码

Rails + PostgreSQL + rbenv-vars: fe_sendauth: no password supplied using environment variables

我正在开发一个使用 PostgreSQL 的 Rails 项目,我想将数据库登录详细信息存储在一个文件中,并使用 rbenv-vars 通过环境变量访问它们。我是 运行 Ubuntu 16.10.

正在考虑以下文件:

pg_hba.conf

# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres        ::1/128                 md5

database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  host: localhost
  pool: 5
  username: consul
  password: <% ENV['CONSUL_DATABASE_PASSWORD'] %>

.rbenv-vars

SECRET_KEY_BASE=(secret key)
CONSUL_DATABASE_PASSWORD=(database password)

当我尝试使用 rake db:create 命令为项目创建数据库时出现此错误:

fe_sendauth: no password supplied

我试过以明文形式存储密码并且它有效,但正如你想象的那样,我不想那样做。

fe_sendauth: no password supplied

<% ENV['CONSUL_DATABASE_PASSWORD'] %>

应该是

<%= ENV['CONSUL_DATABASE_PASSWORD'] %>

将行更改为

password: <%= ENV['CONSUL_DATABASE_PASSWORD'] %>

<% %> 只评估内容而

<%= %> 评估并插入内容到标签的位置

有关 erb 标签的更多详细信息,请参阅此答案What is the difference between <%, <%=, <%# and -%> in ERB in Rails?

就我而言,问题是我的环境变量名称中有 破折号而不是下划线 ,即:

<%= ENV['APP-NAME_DATABASE_PASSWORD'] %>

config/database.yml 中的变量名称更改为 APP_NAME_DATABASE_PASSWORD 为我修复了它。