Canot connect to postgresql server from out - postgresql

I want to connect to postgresql from another server but occur below error. i search and find some result like below but after do these again error occured
in postgresql.conf change listen_addresses from localhost to *
in pg_hba.conf change 127.0.0.1/32 to 0.0.0.0/0
reboot server
Error:
could not connect to server: Connection refused.Is the server running
on host "Ip Number"
and accepting.TCP/IP connections on port 5432?

I suggest that you should do as the following steps.
Check your PostgreSQL SERVER is running.
Check the listen_addresses parameter ( postgresql.conf )
Check the port number of your postgresql ( The port parameter of
postgresql.conf )
Check your the firewall policy of your database server , set
that allow the port of your postgresql.
If you want to connect your db from another server, may be you
shoud set the pg_hba.conf as following :
# TYPE DATABASE USER CIDR-ADDRESS METHOD
local all all trust
host all all 0.0.0.0/0 md5

Try to check if the network traffic reaches the server. You can sniff the network traffic to see what happen. Meassure on client and server and check the communication.
sudo tcpdump -A -n -s 0 -w test_on_client.log port 5432
After you try your connect stop tcpdump with CTRL-C and inspect the log file with Wireshark.
This procedure will find every network connection problems.

Related

Remote connection to postgresql in pgAdmin4

I am trying to connect to a database from a remote computer. According to the guides, I configured the files in the pg_hba file by setting 0.0.0.0/0 for IPv4, similarly for IPv6 and for the file in PostgreSQL listen_addresses = '*'. The firewall allowed any connections to port 5432. I use pgAdmin try 4 and in the server settings - the host/addresses for some reason remained on localhost, every time I try to insert my network address there (in X.X.X.0 does not work), a long download occurs, after which the password for the Postgres database user is requested and the error "connecting to the server on "H.H.H.0", port 5432 failed...". At the same time, everything works on localhost. What am I doing wrong?

Can't connect to postgres database from remote host using psycopg2?

I am unable to connect to postgres database through remote host using psycopg2 and getting the error like
Error:
File "/usr/lib64/python2.7/site-packages/psycopg2/__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection timed out
Is the server running on host "192.x.x.x" and accepting
TCP/IP connections on port 5432?
Note: I have made following changes
1.updated the pg_hba config file
host all all 0.0.0.1/32 trust
2.updated the postgresql file
listen_addresses = '*'
3.changed the firewall rule to allow connection from port 5432
What else I have to do to make it work?
For others that might encounter the same issue, here are some things you can try.
Make sure the service is listening on the needed interfaces
sudo ss -lntp | grep 5432
If you see something like 127.0.0.1:5432 or ::1:5432 this means localhost.
Tweak postgresql.conf:
listen_addresses='192.168.1.2, 127.0.0.1'
You can also use '*' which means any interface but depending on your network configuration it might be dangerous.
Make sure the listen_addresses line isn't commented (as was the case above) and restart the service after making changes.
Check your firewall configuration
After making sure the service is listening on the correct interface make sure your firewall(s), if any, permit the client to connect to the service (this can mean your local firewall, a network device sitting between the client and the service).
Check pg_hba.conf
This file controls PostgreSQL's host-based authentication mechanism:
# TYPE DATABASE USER ADDRESS METHOD
host all all 192.168.1.3/32 md5
The line above allows all users to connect from 192.168.1.3 if they provide the required password; if you want to allow a whole subnet you can use something like 192.168.1.0/24.
If you suspect an issue with psycopg2 or Python you can test the connectivity using PostgreSQL's client: psql:
psql -U postgres -h 192.168.1.2 db_name

Cannot connect to postgresql remotely

I cannot connect to my postgresql instance remotely on port 5432. Connection times out. I cannot connect remotely using psql, pgAdmin or telnet. I feel like I have everything configured correctly. Postgresql has been restarted several times with these settings. Remote connection works fine.
What could I be missing?
netstat:
postgressql.conf
pg_hba.conf:
Try referring this link for help
https://www.thegeekstuff.com/2014/02/enable-remote-postgresql-connection/?utm_source=tuicool
in short you have to the following steps
1. Add the following line to the pg_hba.conf server. This will allow connection from “192.168.xxx.xx” ip-address (This is the client ip)
# vi /var/lib/pgsql/data/pg_hba.conf
host all all 192.168.xxx.xx/xx trust
Change the Listen Address in postgresql.conf
# grep listen /var/lib/pgsql/data/postgresql.conf
listen_addresses = '*'
Test the Remote Connection
You just need to change the method md5 to trust and and enter the address as 0.0.0.0/0.
Open the config file
vi /var/lib/pgsql/data/pg_hba.conf
Insert the following line
host all all 0.0.0.0/0 trust
Maybe it will work.

Heroku Postgresql pg:psql No Route to host

For now I am just trying to make the heroku pg:psql command work but my final purpose is to copy a database that I have on my computer (localhost) to the heroku postgresql database with the pg:push command.
For now when I simply try to access the database that I created on heroku, the heroku pg:psql command returns:
psql: could not connect to server: No route to host
Is the server running on host "ec*-**-***-***-**.eu-west-1.compute.amazonaws.com" (**.***.***.**) and accepting
TCP/IP connections on port 5432?
postgresql.conf: (the lines are not commented)
listen_address ='*'
port = 5432
ssl = true
and host all all **.***.***.** trust in pg_hba.conf
I also tried to add rules to iptables in order to give access to the database from the host IP address provided by heroku.
I am on a Debian computer, how can I solve this?
psql: could not connect to server: No route to host
It means your PostgreSQL server is not starting up or is starting up on a different port.
Solutions you may try:
Check PostgreSQL service by command ps -ef | grep Postgres.
Check the port which PostgreSQL is listening to by command netstat -tupln | grep Postgres.
Make sure your server enables UDP port because PostgreSQL needs UDP port loopback for stats collector service.
Check the startup logs or database logs at pg_log about the problem.

Postgres PgAdmin 3: Server doesn't listen - Tried everything

I tried opening ports in IPTables,
Set Listen address to *
Added
host all all 23.81.27.0/24 trust
and even
host all all 0.0.0.0/0 trust
YouGetSignal shows the port is closed, and i can't connect to my DB via PgAdmin.
I get
could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "23.81.27.206" and accepting TCP/IP
connections on port 5432?
Any ideas?
I already tried to reboot Postgresql, IPtables, and the server.
With this error
could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "23.81.27.206" and accepting TCP/IP
connections on port 5432?
no need to modify hba_file. Either listen_address in postgres.conf not set to listen on external iface, or firewall blocks it/ not translating/ othernetwork issues. First case often people modify listen_address in wrong postgres.conf or do not restart server. To make sure it did not happen to you, ssh to server, psql to it and check
show config_file;
show listen_addresses;
If those ok, then still on remotes shell :
psql -h 23.81.27.206
If you get connected, stop looking into postgres config - check network (firewall, PAT,NAT,routes, your client connection ((maybe you not connected to the Internet?..)))
Turns out it was IPTables after all. I did "Service iptables stop" And i can connect. I can't manage to allow my ip/open that port. But i guess this is how i have to do it. i just enable it whenever im done.