Rails6 New app Postgres Database Connection Issue Peer authentication failed for user

44 views Asked by At

I'm using rails 6.1 and ruby 3.1.1 on ubuntu 22.04

I've started a new rails app using Postgres: rails new lmt --database=postgresql

I also ran gem install pg

I have postgres on my local machine and have previously created the dbs: lmt_production, lmt_development, and lmt_test. They all use the user lmt. lmt was granted all privileges on all dbs.

When I run rails s, it generated this error:

ActiveRecord::ConnectionNotEstablished (connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  role "jdc44" does not exist
):

I added the lmt username to the database.yml for all three dbs and now I get this error:

ActiveRecord::ConnectionNotEstablished (connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  Peer authentication failed for user "lmt"
):

I understand the user issue, but can't find any help on peer.

This is all on my dev site (local machine). I haven't pushed anything to heroku yet.

I had another app that used this same database. I did not have any issues with that setup.

Thoughts?

My Database.yml file:

# hard-coding the database and username for testing

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: lmt_development
  username: lmt

test:
  <<: *default
  database: lmt_test
  username: lmt

production:
  <<: *default
  database: lmt_production
  username: lmt
  password: <%= ENV['MY_DATABASE_PASSWORD'] %>

PG::ConnectionBad FATAL: role "Myname" does not exist

1

There are 1 answers

0
John Cowan On

I figured it out by comparing my old app that used postres. It took awhile to see the obvious. The database.yml in the new app did not have a localhost entry.

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

  host: localhost
  username: lmt
  password: <%= ENV['MY_DATABASE_PASSWORD'] %>

Sigh!