Unable to connect from local psql to postgresql using ip address - postgresql

I am trying to connect from a psql session to a locally running postgresql:
psql -h 192.168.195.84 -d aact_bac
The pg_hba.conf entry is:
host all all 0.0.0.0/0 md5
host all all 192.168.195.84/0 md5
Here is the result:
psql: FATAL: psql: FATAL: no pg_hba.conf entry for host "192.168.195.84",
user "pointr", database "aact_back", SSL o for host "192.168.195.84",
user "pointr", database "aact_back", SSL o
What is missing here?
Update The following had been done several times - after each attempt for change in pg_hba.conf:
sudo service postgresql restart
It was verified that the pg server had been restarted by seeing the launch time from ps -ef | grep postgres.

Here is best answer for you, if it works, this is duplicate answer.
If you don't leave comment. :D
I think this is you missed.
# content in pg_hba.conf
# IPv4 local connections:
host all all 0.0.0.0/0 md5
Remote PostgreSQL connection with pgAdmin

Related

PostgreSQL local database access for user without password

I just create a user and new database on my local machine.
sudo su - postgres -c "createuser gauthier"
sudo su - postgres -c "createdb local-ag-db"
Then:
$ sudo -u postgres psql
sudo -u postgres psql
psql (11.9 (Debian 11.9-0+deb10u1))
Type "help" for help.
postgres=# grant all privileges on database "local-ag-db" to gauthier;
GRANT
I can access the database locally through a socket:
$ psql local-ag-db gauthier
psql (11.9 (Debian 11.9-0+deb10u1))
Type "help" for help.
local-ag-db=> \conninfo
You are connected to database "local-ag-db" as user "gauthier" via socket in "/var/run/postgresql" at port "5432".
But I cannot access it through localhost:
$ psql -d local-ag-db -h localhost -U gauthier
Password for user gauthier:
psql: fe_sendauth: no password supplied
My guess is that while createuser accepted to create a user without a password, logging in through the host requires identification that this user cannot provide.
This is the default content of /etc/postgresql/11/main/pg_hba.conf:
# 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 all peer
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
I have tried adding this at the end of that file, after reading the included documentation:
host local-ag-db gauthier samehost peer
but that did not work (after a restart sudo /etc/init.d/postgresql restart), it even broke my Unix socket access:
$ psql local-ag-db gauthier
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I also tried this as last row of my config, with the same result:
host local-ag-db gauthier 127.0.0.1/32 peer
My questions:
how do I allow my local user to access the database through localhost?
how do I edit my conf file so that I don't break everything? (Unix socket method stops working when I add a rule)
On Debian.
You will need to add a line like:
host all all localhost trust
As you have no explicit rule currently it is dropping through to another catch all rule and prompting for authentication. Adrian's comment above is saying the same and likewise I don't suggest using trust outside of testing your connectivity - you really should assign a password and enforce it.
Adding entries to the pg_hba.conf needs care as the PostgreSQL will find the first match:
The first record with a matching connection type, client address, requested database, and user name is used to perform authentication. There is no “fall-through” or “backup”: if one record is chosen and the authentication fails, subsequent records are not considered. If no record matches, access is denied.
From https://www.postgresql.org/docs/current/auth-pg-hba-conf.html
Regarding
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Looks like the restart may not have worked. Looks like no response rather than authentication failure.
Your server is not running. It never came back up when you tried to restart it, because you broke your hba. If you look in the log file, you should something like:
LOG: peer authentication is only supported on local sockets
FATAL: could not load pg_hba.conf
LOG: database system is shut down
Unbreak your pg_hba.conf and start the server again. Also, read your log files when you don't know what's going on. That is what they are there for.
You will need to choose a different method for authentication over localhost. ident is equivalent to peer, but it requires you to run the identd daemon on the server, and is not very secure for any addresses other than loopback ones.

Postgres asking for forgotten password (MacOS)

I'm completely unable to access anything in postgres as it's asking for a password for both the default postgres user as well as my mac username. I don't have a password for either, and in the case of postgres I don't even understand why there is a password as by defualt it shouldn't have one.
After running through many other answers on the subject:
If I run psql:
$ psql
Password for user <MY_USERNAME>:
psql: error: could not connect to server: FATAL: password authentication failed for user "<MY_USERNAME>"
If I run it with sudo:
$ sudo psql
Password for user root:
psql: error: could not connect to server: FATAL: password authentication failed for user "root"
If I run it with postgres user:
$ sudo -u postgres psql
Password for user postgres:
psql: error: could not connect to server: FATAL: password authentication failed for user "postgres"
Also in my pg_hba.conf all the methods are set to trust, have also tried changing to ident and md5.
On the advice of #jjanes I searched for other pg_hba.conf files and this is what I found:
/usr/local/var/postgres.old/pg_hba.conf
/usr/local/var/postgres/pg_hba.conf
/usr/local/var/postgresql#10/pg_hba.conf
/Library/PostgreSQL/12/data/pg_hba.conf
The last one is empty (and read only) and the other three all look like this:
# TYPE DATABASE USER ADDRESS METHOD
# "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
# 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
Anyone have any advice?
I had a similar issue on my Ubuntu 21.10.
I also tried resolving the problem by following the steps suggested above, but none worked.
At last, I resolved the problem by uninstalling postgres and removing every related files: https://askubuntu.com/questions/32730/how-to-remove-postgres-from-my-installation/32735#32735
Thereafter, I did a new installation of postgres using: https://levelup.gitconnected.com/installing-and-configuring-postgresql-on-ubuntu-20-04-e50122635927
Now, everything works fine.
Cheers.

Not able to connect to postgres on ec2

I just installed PostgreSQL 9.2 server on an EC2 AMI instance. However I am not able to connect to it from the command prompt.
Moreover I see two directories in /var/lib: pgsql9 and pgsql92. The data directory in pgsql92 is empty and hence it looks like pgsql9 is the one that is getting used.
[root#ip-172-31-56-103 etc]# psql
Password:
psql: FATAL: password authentication failed for user "root"
[root#ip-172-31-56-103 etc]# sudo su - postgres
-bash-4.2$ psql
Password:
psql: FATAL: password authentication failed for user "postgres"
-bash-4.2$ psql -U postgres
Password for user postgres:
psql: FATAL: password authentication failed for user "postgres"
-bash-4.2$
pg_hba.conf
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres peer
#host replication postgres 127.0.0.1/32 ident
#host replication postgres ::1/128 ident
postgresql.conf
listen_addresses = '*'
To work out which PG install you are using: ps -ef | grep pgsql. You will see for sure which binary, data directory and conf file is being used to give you some comfort.
Have you changed the pg_hba.conf from the default? If so, did you reload it? Something like sudo service postgresql reload should do it, depending upon your OS.
You might want to change the IPv6 local connection to use md5 as well.
Try adding -h localhost or -h 127.0.0.1 to your psql command: e.g. psql -h localhost -U postgres.
Check your postgres password to be doubly / triply sure.
Otherwise, check out the specific docs for your OSs installation. Sometimes apt or yum repos do some additional security configuration for you.
Finally, worst case, change all the pg_hba.conf auth methods to trust, then restart the database, logon, change the postgres password, logout, change the auth methods to md5, reload and try to logon again.

PostgreSQL: MD5 Authentication in pg_hba.conf gives me FATAL: Peer authentication failed for user "postgres"

I'm running Ubuntu 14.04 and installed PostgreSQL 9.3.
Edited /etc/postgresql/9.3/main/pg_hba.conf as:
# "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
I restarted the server and now I'd like to login into postgres as postgres:
victor#workstation:~$ psql -U postgres
psql: FATAL: Peer authentication failed for user "postgres"
Shouldn't postgres prompt for my password, as I've set its authentication method to MD5 ?
I understand that if I add -h localhost it will work.
Does not adding this flag causes psql to use peer-authentication?
You must have another local ... peer line above those. The error Peer authentication failed comes from the server, and it's the server tha that picks the auth method.
Other possibilities would be:
Didn't actually restart the server
Edited the wrong pg_hba.conf file if there are multiple PostgreSQL instances on the system.
1) Open -> /etc/phppgadmin -> config.inc.php from your Terminal.
2) Find Below Line and Change it to False.
$conf['extra_login_security'] = true; to $conf['extra_login_security'] = false;
3) After changing Restart your Server.it will resolve your issue.
/etc/init.d/httpd restart

Postgres password authentication issue

I've installed PostgreSQL 9.1 and pgadmin3 on Ubuntu Server 13.10.
I configured postgresql.conf with: listen_addresses = '*'
also I configured ph_hba.conf by changed peer connections to md5
Plus I reset the password of postgres by: sudo password postgres
then restarted the service with sudo /etc/init.d/postgresql restart
after that I tried to connect to the default PostgreSQL template database:
sudo -u postgres psql template1
but login failed with this error message:
psql: FATAL: password authentication failed for user "postgres"
then I tried to login from the pgadmin, which gave me the same error.
I've read here that it might be a password expiry dates bug
PostgreSQL user can not connect to server after changing password
but I couldn't solve it coz I cannot login with psql. Does anyone now how to resolve this issue?
EDIT
ph_hba file:
local all postgres md5
local all all md5
local all all trust
host all all 127.0.0.1/32 md5
hostssl all all 192.168.0.0/16 trust
host all all 192.168.0.0/16 trust
host all all ::1/128 md5
in your pg_hba.conf, modify
local all postgres md5
to
local all postgres trust
then you'll be able to login without password