Ben
Ben

Reputation: 5361

capistrano, rails, PG::ConnectionBad: fe_sendauth: no password supplied

I'm trying to run Capistrano for a rails app with Postgres on Ubuntu 14, I ran into a password error during rake db:migrate-

DEBUG [2823f146] Command: cd /home/ben/apps/mll/releases/20160414014303 && ( export RBENV_ROOT="$HOME/.rbenv" RBENV_VERSION="2.0.0-p645" RAILS_ENV="production" ; $HOME/.rbenv/bin/rbenv exec bundle exec rake db:migrate )
DEBUG [2823f146]  rake aborted!
PG::ConnectionBad: fe_sendauth: no password supplied

I tried every solution to similar posts but no luck. For kicks I also tried running just that command in the remote app dir and got the following:

PG::ConnectionBad: FATAL:  password authentication failed for user "mll"
FATAL:  password authentication failed for user "mll"

This is interesting because it's using my database name as my username. See database.yml below, so for the hell of it I added a mll role, and lo and behold it worked when just running rake db:migrate. I tried running Capistrano again though with this new role, and still no luck.

Is it a reasonable guess that the username is not being accessed/stored properly? Any way for me to test that? I manually ALTER ROLE ben WITH PASSWORD 'mypw'; for both my ben and mll roles, and nothing.

My database.yml:

defaults: &default
  adapter: sqlite3
  encoding: utf8

development:
  <<: *default
  database: db/development.sqlite3

test:
  <<: *default
  database: db/development.sqlite3_test

production:
  <<: *default
  host: localhost
  adapter: postgresql
  encoding: utf8
  database: mll
  pool: 5
  username: <%= ENV['DATABASE_USER'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>

\du:

                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 ben       | Superuser, Create role, Create DB              | {}
 mll       | Superuser, Create role, Create DB              | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}

I read changing md5 to trust helped some people, I tried that, but I'm unsure how to restart, all commands I've seen haven't worked for me.

pg_hba.conf:

local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD
local   all             all                                     peer
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5

Upvotes: 3

Views: 1851

Answers (2)

Anjankumar H N
Anjankumar H N

Reputation: 181

The best practice to resolve the issue is -

  1. Add the below gem in your Gemfile under development group and bundle install.

    gem 'capistrano-postgresql'

  2. Add the below line in Capfile

    require 'capistrano/postgresql'

  3. Add below lines in config/deploy.rb or in config/deploy/*.rb

    set :pg_password, ENV['DATABASE_PASSWORD']

    set :pg_ask_for_password, true

Upvotes: 0

Anthony E
Anthony E

Reputation: 11235

You should use "trust" under method for localhost in pg_hba.conf. Note that this means all connections from localhost will be able to login as any user, which is probably fine as long as you're using this for development.

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

After changing your pg_hba.conf you can restart postgres with pg_ctl reload

Upvotes: 3

Related Questions