what do I have to change in my pg_hba.conf to get rid of peer authentication error? - postgresql

I have postgres running on a raspberry pi, and I was getting the old
psycopg2.OperationalError: FATAL: Peer authentication failed for user "george"
The user george is a user I made that can create and access databases, but can't create users. What do I have to change in my pg_hba.conf file so that george can connect to a dabatabse using a password. This is what my file looks like:
# 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
This is what my connection line looks like in my python test program:
conn = psycopg2.connect(database="homebot", user="george", password="xxxxxx", port=5432)
This is what I do on the commandline to get into psql
psql george -h 127.0.0.1 -d homebase
Which works, so....what do I have to change / do to get that connection line to not throw an error and connect?

Add a new line to the configuration file setting the authentication method for george to md5.
local all george md5

Related

Error message with pg_dump and PostgreSQL 14

We use PostgreSQL for our database of Speedy Net. I had a production server with PostgreSQL 13, and I backed up my database with the following command:
pg_dump -U postgres -h localhost "speedy_net" | gzip > <file_name>.sql.gz
It worked with PostgreSQL 13, but recently I upgraded my server to Ubuntu 22.04.1 LTS and PostgreSQL 14. I loaded the database from SQL and everything works properly, but now I can't backup the database - I get the following error message:
pg_dump: error: connection to server at "localhost" (::1), port 5432 failed: fe_sendauth: no password supplied
Now, something has changed in the configuration file from PostgreSQL 13 to 14. In PostgreSQL 13, the file /etc/postgresql/13/main/pg_hba.conf was:
# 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 trust
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
And in PostgreSQL 14 the file /etc/postgresql/14/main/pg_hba.conf is:
# Database administrative login by Unix domain socket
local all postgres peer
local all speedy_net trust
# 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 trust
# 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
Notice, the line local all speedy_net trust is new. Is this line causing problems? I had to add it because otherwise I couldn't load the database from SQL.
Do you know how I fix it so that backups will work? If I run the command pg_dump -U postgres -h localhost "speedy_net" manually I get prompted for password, but there is no password and it worked with PostgreSQL 13.
Maybe I need to change the password? How do I do it?
The line that is causing the problems in the new file is
host all all ::1/128 scram-sha-256
That requires you to supply a password. You have two choices:
Use a different authentication method. trust would allow you to login without a password (since you are using it with “localhost” in IPv4, why not with IPv6?). If you don't want that, certificate authentication would be an option.
Create a password file on the new machine. Since you forced password authentication on the old machine, that's probably what you used there.

postgresql error User "redacted_user" has no password assigned. NOT USING DOCKER

So I'm editing my original post and reducing it a bit.
I have a server running Ubuntu 22.04LTS and postgresql 10. I have installed an application on it that requires access to the dbase via it's own dbase user, password and the dbase "gitea".
I created a user "gitea" with a password in postrgesql and I can access it locally using the following command:
psql gitea gitea
My pg_hba.conf file is:
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
host all bugs 127.0.0.1 255.255.255.255 md5
host all gitea 127.0.0.1 255.255.255.255 md5
# "local" is for Unix domain socket connections only
local all all trust
# 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
So why does this allow me to access the dbase without a password? Is it an easy change to make sure that the user "gitea" always has to use a password whether it's local or remote?
Cheers!!

postgres 10 Peer authentication failed

I'm trying to log in to a database with this command:
psql -U user my_db
This is the error that gets thrown:
psql: FATAL: Peer authentication failed for user "user"
I figured it had something to do with my pg_hba.conf. I tried several things but couldn't get it to work. This is the relevant part of pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local my_db user md5
local all all md5
# IPv4 local connections:
host my_db user 127.0.0.1/32 md5
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident
I also get the same error when I first do su - postgres
and the try psql -U user
Edit: Turns out I was modifying the wrong pg_hba.conf. I solved the original problem, now I can log in like this: psql -U user my_db. But my application, which is running on the same device can't access the database with this pg_hba.conf. Checking the logs this is the line that shows up when my application makes new requests:
2019-12-10 16:18:51.144 +03 [29570] LOG: invalid length of startup packet

postgresql: Failing to connect locally via pgadmin3 (and phppgadmin)

I am able to connect via command line but getting the usual error message in pgadmin3.
Error connecting to the server: FATAL: password authentication failed for user "postgres"
(and login failed in phppgadmin)
I know there are several q/a about this but none is addressing command-line success / pgadmin3 failure.
I have made the suggested changes such as:
adding listen_addresses = '*' in postgresql.conf
as also modifying trailing lines of pg_hba.conf as follows (to switch from peer to md5):
# "local" is for Unix domain socket connections only
local all all md5
# 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
what is more, connection in the following way fails:
psql -h localhost -U postgres -d postgres
(password failure)
however, connection like this succeeds:
sudo -u postgres psql
Password: ****
It turns out that the server was listening to 5433 and both GUIs were trying to connect to 5432... the error message
password authentication failed
was very misleading though....

Postgresql and host

I'm trying to deploy my Rails application through Capistrano 3.
On my own server everything was working but with my new one, I have some problem to access to the database :
DEBUG [05c115bf] FATAL: no pg_hba.conf entry for host "ip.address.of.the.server", user "postgres", database "my_database", SSL on
DEBUG [05c115bf] FATAL: no pg_hba.conf entry for host "ip.address.of.the.server", user "postgres", database "my_database", SSL off
I know that the problem comes form pg_hba but I cannot find where exactly.
This is what I've tried :
# Database administrative login by Unix domain socket
local all postgres md5
# 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
hostssl all all ip.address.of.the.server/24 md5
hostnossl all all ip.address.of.the.server/24 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
Currently I'm blocked at this point.
put real IP address of the server with which you want to connect instead if ip.address.of.the.server for more info visit pg_hba.conf
The problem came from my deploy.rb where I made a mistake on the host... Simply.
After hours of researches, this problem is finally fixed. Thank you for your help.