title: Rails: fe_sendauth: no password supplied (PG::ConnectionBad) from Ruby, but ok in Rails
body: <p>I'm having problems assessing a postgres database from straight ruby.</p>
  
  <p>I've created a Postgres database using Rails</p>
  
  <pre><code>&gt;rails new www --database=postgresql
  </code></pre>
  
  <p>using Rails 4.2.5 and Postgres is 9.4</p>
  
  <p>It produces the following <strong>config/database.yml</strong> file.</p>
  
  <pre><code>default: &amp;default
    adapter: postgresql
    encoding: unicode
    pool: 5
  
  development:
    &lt;&lt;: *default
    database: www_development
  
  test:
    &lt;&lt;: *default
    database: www_test
  
  production:
    &lt;&lt;: *default
    database: www_production
    username: www
    password: &lt;%= ENV['WWW_DATABASE_PASSWORD'] %&gt;
  </code></pre>
  
  <p>I can run rails server, db:drop, db:create and db:migrate fine.</p>
  
  <p>I can also access the database fine with psql</p>
  
  <pre><code>&gt;psql www_development
  </code></pre>
  
  <p><strong>But</strong> when I run the following <strong>app.rb</strong> from a non Rails project directory, I get a <strong><em>fe_sendauth: no password supplied (PG::ConnectionBad)</em></strong> error message.</p>
  
  <p>It's clearly not a Rails issue. I've either missed something in my ruby or Postgres need a tweek to handle some difference between Rails and pure Ruby [that I'm not aware off]. I've also included Postgres' <strong>pg_hba.conf</strong> file.</p>
  
  <p>At wits end trying to figure this one out. Any help would be much appreciated. </p>
  
  <p><strong>app.rb</strong></p>
  
  <pre><code>require 'yaml'
  require 'active_record'
  require 'pg'
  
  ActiveRecord::Base.establish_connection(
    adapter:  'postgresql',
    host:     'localhost',
    database: 'www_development',
  )
  </code></pre>
  
  <p><strong>/etc/postgresql/9.4/main/pg_hba.conf</strong></p>
  
  <pre><code># TYPE  DATABASE        USER            ADDRESS                 METHOD
  
  local   all             postgres                                peer
  local   all             all                                     peer
  host    all             all             127.0.0.1/32            md5
  host    all             all             ::1/128                 md5
  </code></pre>