Can't connect to Postgresql via 127.0.0.1 - postgresql

I'm trying to connect to a local instance of Postgresql using 127.0.0.1 as IP address but the connection is always denied:
⟩ psql -h 127.0.0.1 -U postgres -p 5432
psql: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
My pg_hba.conf looks like this:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all password
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 trust
My postgresql.conf has the following entries:
listen_addresses = 'localhost, 127.0.0.1'
port=5432
unix_socket_directories = '/private/tmp'
I can however connect via the unix socket. What else could be blocking the connection?

Not sure what happened but a hard reboot of my laptop fixed the issue.

Related

After changing the port number, connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"

I finished install postgresql11 and to change port number
The first file I modified was '/var/lib/pgsql/11/data/postgresql.conf'
#listen_addresses = 'localhost' -> listen_addresses ='*'
#port = 5432 -> port = 9485
The second file I modified was '/var/lib/pgsql/11/data/pg_hba.conf'
the line i added is 'host all all 0.0.0.0/0 md5'
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 ident
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 all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident
i restarted postgresql server
systemctl restart postgresql-11
Connection from external client to dbeaver works fine.
but local connection is not fine
su - postgres
psql
there is error message
psql: could not connect to server: There is no such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I can find two files in '/var/run/postgresql'
.s.PGSQL.9485
.s.PGSQL.9485.lock
If Ireturn the port to the original number of 5432, it works normally again.
Please tell me how to fix it
psql is not aware of your Postgres settings, it is using the default port 5432.
Try: psql -p 9485
To complete your setup you have to change environment variable PGPORT
PGPORT=9495; export PGPORT;
If not you have to give port number ( -p 9495 ) to every cde (psql, pg_dump, ...)

Amazon EC2: PostgreSQL remote connection hangs

I'm trying to set up PostgreSQL remote connection on Ubuntu server.
I changed listen_addresses = 'localhost' to listen_addresses = '*' in /etc/postgresql/9.3/main/postgresql.conf
Then I added to postgre config file /etc/postgresql/9.3/main/pg_hba.conf lines
host all all 0.0.0.0/0 md5
host all all ::0/0 md5
host all all all md5
But when I'm trying to connect remote DB by psql -U db_user -h host -d db_name it hangs and show message
psql: could not connect to server: Connection timed out
Is the server running on host "host_address" and accepting
TCP/IP connections on port 5432?
PgAdmin connection hangs as well
Had the same issue using amazon EC2 service
Solution:
1) Go to security group for your server instance
2) Edit inbound rules
3) Allow all traffic for your IP

Ident authentication failed on PostgreSQL through SSH tunnel

I have PostgreSQL server and a seperate computer is a client of it. They are in one network. If I use psql command like
psql --host db_ip_address --port 5432 --user user base_name
connection goes fine and all works.
But if I'll open SSH tunnel to DB server like:
ssh -L 63333:localhost:5432 root#db_ip_address
and then try to do the same like:
psql --host localhost --port 63333 --user user base_name
than it suddenly output error message:
psql: FATAL: Ident authentication failed for user "user"
pg_hba.conf on server have this lines:
# 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
host all all 192.168.0.49/32 trust
host all all 192.168.0.50/32 trust
host all all 192.168.0.48/32 trust
# IPv6 local connections:
host all all ::1/128 ident
I need to use SSH tunnels because I actually need one more tunnel for my own computer, and it is the only way to get connection for db on it.
And I don't want to change any configuration or base on PostgreSQL server, because it is working in real time server.
Hope for help.
Based on the error message and pg_hba.conf, the server resolves localhost to its IPv6 address, which is tied to ident authentication.
As a solution, you may either:
change pg_hba.conf to set trust method for ::1/128, as is already the case for 127.0.0.1/32
or run psql --host 127.0.0.1 --port 63333 [other options]... to force the IPv4 address.

Correct setings for pg_hba.conf

Having difficulty getting Mezzanine to connect to Postgresql via Docker
In my postgresql.conf I have:
listen_addresses = '*'
In my pg_hba.conf I have:
# 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
host all all samenet trust
host all all 0.0.0.0/0 md5
The error I keep getting is:
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
I have PORT in settings.py set to '' but I have PORT defined in postgresql.conf to 5432.
Any suggestions?
Ensure that port mapping is properly configured https://docs.docker.com/userguide/dockerlinks/
If your app is running in one container and postgresql server is running in another container, you need to link containers together.
By the way you are trying to establish connection via localhost and you have md5 authentication method there. For debug purposes change authentication method to trust. You should EDIT this line in your pg_hba.conf NOT to ADD another line.
host all all 127.0.0.1/32 md5
The best way to debug connection issues is to use psql utility.

postgres hba.conf for jdbc

I'm trying to set up a JDBC connection to postgres from another program.
When I run the program, I get the following error:
[2013-03-24 03:14:10,542][ERROR][org.elasticsearch.river.jdbc.strategy.simple.SimpleRiverSource] while opening read connection: jdbc:postgresql://[my postgres server's ip address]:5432/[my database name] FATAL: no pg_hba.conf entry for host "[my client ip address]", user "postgres", database "[my database name]", SSL off
I can connect to the server successfully with a command line client by running:
psql -d [my database name] -U postgres -p 5432 -h [my postgres server ip address]
I think then that I have a unix socket connection that works but need to enable tcp connection in the pg_hba.conf, but it looks like I already have several tcp connections allowed in the pg_hba.conf file below... can anyone advise what I may be doing wrong?
My pg_hba.conf contains the following uncommented lines:
hostssl all all 0.0.0.0/0 md5
host all all 10.0.0.0/8 md5
local all postgres peer
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
If doing devel on local computer and booth program and DB run on the same machine go with:
# IPv4 local connections:
host all all 127.0.0.1/32 trust