9

I've tried to create my Rails app with postgresql database, but when I started rails server, I got this error:

fe_sendauth: no password supplied

Here're my actions step by step:

$ sudo apt-get install postgresql postgresql-contrib
$ gem install pg
$ rails new timetracker --database=postgresql --skip-unit-test

My database.yml file looks like this:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  host: localhost
  username: postgres
  password:

development:
  <<: *default
  database: timetracker_development

test:
  <<: *default
  database: timetracker_test

production:
  <<: *default
  database: timetracker_production

And here's my pg_hba.conf file:

# 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                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

# modified by me.
host    all             all             127.0.0.1/32            trust

host    all             all             127.0.0.1/32            trust

host    all             all             ::1/128                 trust

So, when I do rails server I get the error and then here's what I get by doing this:

$ rails c
$ ActiveRecord::Base.connection.instance_variable_get(:@config)

2.1.5 :001 > ActiveRecord::Base.connection.instance_variable_get(:@config) PG::ConnectionBad: fe_sendauth: no password supplied

from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:655:in initialize' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:655:innew' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:655:in connect' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:242:ininitialize' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in new' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in postgresql_connection' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in new_connection' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in checkout_new_connection' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in acquire_connection' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in block in checkout' from /home/denis/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/monitor.rb:211:in mon_synchronize' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in checkout' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in block in connection' from /home/denis/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/monitor.rb:211:in mon_synchronize' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in connection' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in retrieve_connection' ... 4 levels... from /home/denis/.rvm/gems/ruby-2.1.5/gems/railties-4.2.3/lib/rails/commands/console.rb:9:in start' from /home/denis/.rvm/gems/ruby-2.1.5/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:68:in console' from /home/denis/.rvm/gems/ruby-2.1.5/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in run_command!' from /home/denis/.rvm/gems/ruby-2.1.5/gems/railties-4.2.3/lib/rails/commands.rb:17:in ' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in require' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:inblock in require' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in load_dependency' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:inrequire' from /home/denis/WEB/Rails/timetracker/bin/rails:8:in <top (required)>' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:inload' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in block in load' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:inload_dependency' from /home/denis/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in load' from /home/denis/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:inrequire' from /home/denis/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in require' from -e:1:in'2.1.5 :002 >

I'm using postgresql for the first time and I guess I'm just doing something wrong.

Could you please help me with that?

Denis Yakovenko
  • 3,241
  • 6
  • 48
  • 82
  • 1
    Move your amendments to `pg_hba.conf` above the pre-supplied ones. Also you have a duplicate entry. – Colonel Thirty Two Jul 23 '15 at 16:49
  • @ColonelThirtyTwo Unfortunately, this didn't change anything. Still getting the same error... – Denis Yakovenko Jul 23 '15 at 16:55
  • 1
    Also do `sudo service postgresql reload` or reboot to load the changes – Colonel Thirty Two Jul 23 '15 at 16:56
  • @ColonelThirtyTwo, thanks, I guess it worked, but I started to get the new error: `FATAL: database "timetracker_development" does not exist`. Should I actually create all the databases manually (I thought rails would do that for me) ? – Denis Yakovenko Jul 23 '15 at 16:58
  • Probably. Not familiar with rails, but most ORMs I've seen only create tables and such, not the actual databases. – Colonel Thirty Two Jul 23 '15 at 17:01
  • @ColonelThirtyTwo Tried to create the database manually and everything worked fine! Your answers have been really helpful. PS: you may want to post your answers as the actual answer and I'll tick it as the accepted one – Denis Yakovenko Jul 23 '15 at 17:07
  • I know this is a year late, but for posterity, after getting `FATAL: database "database_name" does not exist`, try running `rake db:create:all`. If _that_ doesn't work (i.e. you have insufficient permissions), try following the instructions [here](http://stackoverflow.com/questions/28116927/postgres-permission-denied-to-create-database-on-rake-dbcreateall). – MK McKenzie Jul 01 '16 at 01:54

3 Answers3

11

PostgreSQL matches pg_hba.conf entries in the order they are specified, so it uses the pre-specified host all all 127.0.0.1/32 md5 entry for authentication instead of your specified trust entry.

Move your custom trust entries above the pre-specified ones (and optionally comment out the old ones) and then reload your configuration by running sudo service postgresql reload (on Ubuntu) or rebooting.

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
11

My solution to this problem was to remove host: localhost from the default: group.

jljohnstone
  • 1,202
  • 1
  • 10
  • 14
  • 4
    The reason why this works is that without defined `host` Unix socket is used. During authentication it will match `peer` rule in `pg_hba.conf`, thus no password is needed (your username `whoami` must match a role in DB). – Tombart Feb 26 '19 at 12:56
1

I don't know why, but for me, this line did not solve the problem

host    mydb       myuser         127.0.0.1/32            trust

while this one did :

host    mydb       myuser         localhost               trust
PierreL
  • 169
  • 9