Rails: fe_sendauth: no password supplied (PG::ConnectionBad) from Ruby, but ok in Rails

10,324

Solution 1

You don't specify neither username, no password in your ActiveRecord::Base.establish_connection(. I assume you want to use some SUPERUSER without password for www_development database - right?

as per documentation peer auth does

Obtain the client's operating system user name from the operating system and check if it matches the requested database user name.

That is why if you can psql without password, you should be able run app.rb with same OS user and environment without password. If you can't, then app.rb tries to connect with different username or so...

Options:

  1. put username: postgres to ActiveRecord::Base.establish_connection(

  2. change local all all peer to local all all trust

Solution 2

With ENV variable as password it's very likely that the variable itself is not present. Confirm the presence with printenv. You need to relogin/reboot for the variable to be accessible after you've included it in /etc/environment file for example. If this works, it's probably better than changing pg_hba.conf.

Share:
10,324
gnoll110
Author by

gnoll110

Rubyist.

Updated on June 04, 2022

Comments

  • gnoll110
    gnoll110 almost 2 years

    I'm having problems assessing a postgres database from straight ruby.

    I've created a Postgres database using Rails

    >rails new www --database=postgresql
    

    using Rails 4.2.5 and Postgres is 9.4

    It produces the following config/database.yml file.

    default: &default
      adapter: postgresql
      encoding: unicode
      pool: 5
    
    development:
      <<: *default
      database: www_development
    
    test:
      <<: *default
      database: www_test
    
    production:
      <<: *default
      database: www_production
      username: www
      password: <%= ENV['WWW_DATABASE_PASSWORD'] %>
    

    I can run rails server, db:drop, db:create and db:migrate fine.

    I can also access the database fine with psql

    >psql www_development
    

    But when I run the following app.rb from a non Rails project directory, I get a fe_sendauth: no password supplied (PG::ConnectionBad) error message.

    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' pg_hba.conf file.

    At wits end trying to figure this one out. Any help would be much appreciated.

    app.rb

    require 'yaml'
    require 'active_record'
    require 'pg'
    
    ActiveRecord::Base.establish_connection(
      adapter:  'postgresql',
      host:     'localhost',
      database: 'www_development',
    )
    

    /etc/postgresql/9.4/main/pg_hba.conf

    # 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