ruby-on-railsrubypostgresqlubuntu-16.10rbenv-vars

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


I'm working on a Rails project which uses PostgreSQL and I want to store the database login details in a file and access them with environment variables using rbenv-vars. I'm running Ubuntu 16.10.

Considering the following files:

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)

When I try to create the database for the project using the rake db:create command I get this error:

fe_sendauth: no password supplied

I tried storing the password in cleartext and it works, but as you can imagine, I don't want to do that.


Solution

  • Change the line to

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

    <% %> just evaluate the content whereas

    <%= %> evaluates and insert the content in the place of the tag

    Refer this answer for more details on erb tags What is the difference between <%, <%=, <%# and -%> in ERB in Rails?