Debugging a PostgreSQL Remote Connection - postgresql

I'm having issues connecting to my PostgreSQL database from an online source.
PostgreSQL seems to be setup up fine it is running on it's default port 5432 and the postgresql.conf has the following line
listen_addresses = '*'
And the pg_hba.conf has the following
host dbname usname all md5
I can connect to the PostgreSQL database from a different machine using the following credentials in pgadmin
Name local
Host 192.xx.xx.xx
Port 5432
Maintenance DB dbname
Username usname
Password psword
Using these I can connect perfectly it seems to work fine.
Then I forwarded the port 192.xx.xx.xx:5432 on the ADSL router and checked it using the public IP address 197.xx.xx.xx (visible to the internet IP)
http://www.canyouseeme.org/
Where I can see the port was forwarded ok.
However when I try to connect using
Name online
Host 197.xx.xx.xx
Port 5432
Maintenance DB dbname
Username usname
Password psword
I get the server doesn't listen error message
could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host "197.xx.xx.xx" and accepting TCP/IP connections on port 5432?
Why is this? Do I need to add an extra step to making it accept connections through port forwarding?

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?

Problem to connect to an AWS postgres instance via a bastion host via SSHTunnel in python

I have an AWS postgres instance and an AWS ec2 instance (running as bastion host). From my local machine I want to connect to that AWS postgres instance via a python script. But it's not working (see below).
My setup is:
with SSHTunnelForwarder(
('internet-adress-of-the-basion-host', 22),
ssh_private_key=r"the-private-key.pem",
ssh_username="ec2-user",
local_bind_address=('localhost', 6543),
remote_bind_address=('internet-adress-of-the-postgres-server', 5432)) as server:
server.start()
print("server connected")
self.conpostgres = psycopg2.connect(host='localhost', port=server.local_bind_port, database="the-database",
user="the-user", password="the-password")
I can sucessfully connect via the sshtunnel (the "server connected" messages is printed to the console), but I get an ERROR in the when excecuting psycopg2.connect.
The connect ERROR is:
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 6543?
FATAL: password authentication failed for user "the-user"
FATAL: no pg_hba.conf entry for host "x.x.x.x", user "the-user", database "the-database", SSL off
Two remarks:
1.) In the line "no pg_hba.conf entry for host "x.x.x.x"". Here the "x.x.x.x" address is NOT localhost (127.0.0.1). It's a normal ip4 internet address. But I have no idea where this address is coming from!
2.) !!!! With the exact same configuration mentioned above I can successfully connect to the database via the database tool built in IntelliJ Idea. So there is a way to connect to the postgres server without changing pg_hba.conf. So maybe I have to configure the tunnel differently?

pgadmin4 tries to connect to old port but my default port is 5432 - can't login

i cant login to my pgadmin4 panel, I m using postgre12.
When i installed postgres i put the default port to 1111, but then
I changed the port name to default 5432. I checked the postgresql.conf file and the default port is still 5432.
Now when i go to pgadmin4, and try to login with
username - postgres
password - admin or postgres
it gives me this error:
could not connect to server: Connection refused
(0x0000274D/10061) Is the server running on host "localhost"
(::1) and accepting TCP/IP connections on port 1111? could not
connect to server: Connection refused (0x0000274D/10061) Is the
server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 1111?
why is it still referring to this old port 1111?
The problem is with the PgAdmin4 configuration.
Go to server properties -> connections and change the port to 5432

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.

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