Logs say "fe_sendauth: no password supplied" but rails console working without problems

Viewed 203

I'm trying to deploy rails app with capistrano, nginx and puma. When I visit the app I get errors like this in my production.log:

[11c972c9-c8fb-404f-93c3-9fc614ad815b] ActiveRecord::ConnectionNotEstablished (connection to server at "localhost" (127.0.0.1), port 5432 failed: fe_sendauth: no password supplied
):

When I log to this machine and run bin/rails console it works without any problems, I can create records, query records. Connection to database works without problems.

RAILS_ENV is set to production in /etc/environment

Any idea what could be the problem?

database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  host: localhost
  password: <%= ENV['APP_DATABASE_PASSWORD'] %>
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

production:
  <<: *default
  database: app_production
  username: app

pg_hba.conf

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            scram-sha-256
# IPv6 local connections:
host    all             all             ::1/128                 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256
1 Answers

The solution for me was to delete PostgreSQL completely using,

sudo apt-get --purge remove postgresql\*

Click yes on that pop-up, re-install PostgreSQL using,

sudo apt update

then,

sudo apt install postgresql postgresql-contrib

then,

sudo service postgresql start

Finally, you'll also need to create a database user so that you are able to connect to the database from Rails. First, check what your operating system username is:

whoami

If your username is "stack", for example, you'd need to create a Postgres user with that same name. To do so, run this command to open the Postgres CLI: sudo -u postgres -i

From the Postgres CLI, run this command (replacing "stack" with your username):

createuser -sr stack

Then enter control + d or type logout to exit.

Related