create connection to localhost postgres server - ruby-on-rails

I am creating a rails app and want to use postgresql. I have installed pgadmin, postgres sever on Ubuntu 14.04. I've tried creating a connection through pgAdmin but not able to create it. I am unable to configure pgadmin to create a connection to pg server.
I've tried editing the /etc/postgresql/9.3/main/pg_hba.conf file, but I get the following error:
FATAL: Peer authentication failed for user "postgres"
here is '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
Allow replication connections from localhost, by a user with the
replication privilege.
local replication postgres peer
host replication postgres 127.0.0.1/32 md5
host replication postgres ::1/128 md5
I would be grateful if someone could provide some help with configuration.

This is a VERY common error when you first install postgres, it defaults to using "postgres" as the user. You need to add your current user as a user in Postgres: Postgresql: password authentication failed for user "postgres"
First, you can reset the password for postgres:
sudo -u postgres psql
ALTER USER postgres PASSWORD 'newPassword';
But I would also/instead create a user for your current account (As a Superuser):
CREATE ROLE user_name WITH LOGIN SUPERUSER CREATEDB CREATEROLE REPLICATION;
Edit: The above command must be run from psql, so doing "sudo -u postgres psql" to access postgres is required either way.
Source: https://www.digitalocean.com/community/tutorials/how-to-use-roles-and-manage-grant-permissions-in-postgresql-on-a-vps--2

you can also try connecting through pgAdmin with the following configuration..

Related

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

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
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.

Rails Postgres fe_sendauth: no password supplied error

I have been stuck with the following issue, which is popular question that was asked multiple times but none has worked for me. I see the following error when trying to rake db:create or rails s:
ActiveRecord::ConnectionNotEstablished (fe_sendauth: no password supplied)
I tried to create a new DB user but my psql fails with the following error:
Password for user <root_username>:
psql: error: FATAL: password authentication failed for user "<root_username>"
I've updated my pg_hba.conf file to use the trust method as suggested by many answers:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all <root_username> trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
Finally, this is what my database.yml file looks like:
development:
<<: *default
database: datab_development
#username: datab
#host: localhost
I am using OSX 11 (Big Sur). Been hitting walls since this morning, any help would be greatly appreciated! Thanks!

PG::ConnectionBad (FATAL: password authentication failed for user ) on rails s and DBeaver

Why am I getting this error on when running "rails s" and while trying to access the database via DBeaver? It was running yesterday on DBeaver with the same credentials.
My pg_hba.conf file looks like this, "peer" isn't set anywhere as a method and I've tried "md5":
# "local" is for Unix domain socket connections only
local all all scram-sha-256
# 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 scram-sha-256
host replication all 127.0.0.1/32 scram-sha-256
host replication all ::1/128 scram-sha-256
If I try logging in with psql -U username -W, I enter my password then see this:
psql: error: FATAL: Peer authentication failed for user "ryalto"
Credentials are all correct, I've also tried "sudo service postgresql restart".

Unable to get through Postgres connection with ActiveRecord

For sake of Lord, I'm unable to understand why postgres is complaining about password authentication(using ActiveRecord).
sudo -u postgres psql rdb
This work (no password is asked)
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
But when connecting with an ORM like ActiveRecord it complains.
ActiveRecord::Base.establish_connection(adapter: 'postgresql', username: 'postgres', database: 'rdb')
The server requested password-based authentication, but no password was provided.
This a basic setup and I was expecting this to work all the time. My last resort would be to allow trust or md5(on pg_hba.conf) but before I move ahead with that can some please provide me with some pointer as to why the above did not work.
Just an update postgres is running at localhost
The sudo command is triggering the local all postgres peer restriction, not asking for a password. The ActiveRecord configuration with the postgresql adapter uses this:
:host - Defaults to a Unix-domain socket in /tmp. On machines without
Unix-domain sockets, the default is to connect to localhost.
Do you have working sockets on your machine? Is /tmp writeable? It might be the case that a TCP connection to 127.0.0.1 is used, triggering the password check as indicated by md5.

psql: FATAL: Peer authentication failed for user "blah"

On CentOS, I am trying to use rake to migrate data from sqlite3 to postgresql.
When I try to connect to PostgreSQL using the command line:
psql -d <db> -U <user> -W
I get:
psql: FATAL: Peer authentication failed for user "blah"
For testing I even set method to trust for psql. Here are the contents of my pg_hba.conf:
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
Any ideas what might be wrong?
Thanks
Restart PostgreSQL and if that fails post the full pg_hba.conf for us to look at.
Either PostgreSQL has not reloaded this file or you are looking at the wrong pg_hba.conf.

Resources