Amazon EC2 and PostgreSQL: cannot connect due to time out error - postgresql

I think I followed any possible guide before asking here.
I am trying to remotely connect to a PostgreSQL db installed on a EC2 Linux instance (Ubuntu server 18.04, free tier if it matters).
Here are all the steps I did before hitting a wall and begging for help.
I connected to my instance via SSH
I downloaded and installed postgres following this docs
I created a new used (umberto)
I created a new DB (wondermap)
I created a new OS user with the same name as in step 3 (umberto)
I connected to my new db with my new user from the server and everything woked fine
I edited /etc/postgresql/10/main/pg_hba.conf adding these lines:
host all all 0.0.0.0/0 md5
host all all ::/0 md5
I edited /etc/postgresql/10/main/postgresql.conf changing #listen_addresses = 'localhost' into listen_addresses = '*'
i restarted the postgres server with sudo service postgresql restart
I added a new Inbound rule in the instance security group like so:
TCP port:5432 source:0.0.0.0/0, ::/0
After all, I am trying to connect from my local mahine (either using pgadmin3 or psql) but I still receive a timeout error (host is hidden):
psql -h <PUBLIC-IP-OF-MY-INSTANCE> -U umberto -d wondermap
or even
psql -h <PUBLIC-IP-OF-MY-INSTANCE> -U postgres
psql: could not connect to server: Connection timed out
Is the server running on host "<PUBLIC-IP-OF-MY-INSTANCE>" and accepting
Doing SELECT version(); shows:
PostgreSQL 10.7 (Ubuntu 10.7-0ubuntu0.18.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0, 64-bit
What could be the problem?

Apparently, I stil had to unblock postgres port on server's firewall.
sudo ufw allow 5432/tcp
That did the trick!

Related

Apache Airflow install Postgress as Backend DB - ERROR

I am trying to install Postgres as a backend DB instead of Sqllite. I followed these steps but ran into an error. can someone help, please?
Steps followed:
activated virtual env.
installed postgreSQL
sudo apt-get install postgresql postgresql-contrib
created psql object with:
sudo -u postgres psql
4.create the user and database for the airflow
CREATE USER **** PASSWORD '****';
postgres=# CREATE DATABASE airflow;
postgres=# GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO newt;
updated postgresql.conf with listen_addresses= '*'
added pg_hba.conf with
host db-name user-name ip-address trust
updated airflow.cfg file with
sql_alchemy_conn = postgresql+psycopg2://user-name:password#localhost:5432/db-name
restarted airflow db.
airflow resetdb
After the 8th steps I got below error, what am i doing wrong here? any advise
Error message
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
After changing the pg_hba.conf and postgresql.conf you have to tell PostreSQL that a change has been made. You can do it by either restarting the database, e.g. on Ubuntu
$ sudo service postgresql restart
... or by executing the function pg_reload_conf
SELECT pg_reload_conf():
In case client and database are in different machines, make sure that there is no firewall between them messing things up.
Unrelated note: Also keep in mind that setting a connection to trust in the pg_hba.conf means that no password is required. For testing purposes more than legit but quite dangerous if forgotten in production ;)

Troubles connecting PostgreSQL installed in WSL2 Ubuntu from the Windows 10 host

I'm having big troubles connecting to PostgreSQL, installed in WSL2 Ubuntu, from the Windows 10 host.
Here is what I have done so far.
Set password for postgres user.
Set the following in pg_hpa.conf
local all postgres md5
host all all 0.0.0.0/0 md5
Set the following in postgresql.conf
listen_addresses = '*'
Tested successfully that I can connect with psql -U postgres - so password should be set correctly.
When I try to connect using either pgAdmin or psql from the Windows 10 host, I get the following error.
error: could not connect to server: FATAL: password authentication failed for user "postgres"
Anything else I can change in pg_hpa.conf or other PostgreSQL configurations which might help?
I'm thinking that I could have something to do with that WSL2 has its own IP, even tough that connections from the hosts are made appear coming from localhost. The connection seems to be made, but authentication failed for some reason when it is not made from within Ubuntu.
I later tested with nc -l 5432 and for some reason, port 5432 didn't reach WSL2 on local host, but did on WSL-2 IP. If I use any other free port e.g. 5434 (free both on Windows and WSL-2) it works fine and I can connect to the PostgreSQL service on WSl-2 through localhost on windows.
I didn't change any options since the original posted question - only the port.
Update: It turns out that there indeed was a process from an old install of postgres on Windows which was listening on port 5432. This of cause explains it all. ..:/
As was pointed out by the original poster, ensure that you do not have a Windows service already running and listening on that port. In my case it was a PostgreSQL instance installed as a Windows service. Whatever the underlying networking support, it seems you can have a process/service listening on port 5432 in Windows as well as a separate Linux process listening on port 5432 from WSL2.
It was not enough to stop the Windows PostgreSQL service. Connecting from the Windows pgAdmin4 installation program failed. I also disabled the service from Microsoft Services console.
I needed to also stop and start the WSL2 postgresql. Only then was I successfully able to connect from my Windows pgAdmin4 installation program to the WSL2 postgreSQL

Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432

I install pgadmin4 using the following command sudo install pgadmin4 and then I installed postgresql using sudo install postgresql . In my terminal I ran the command psql -U postgres -h localhost
It shows connection refuse
Then I go to pgadmin4 and created a server and I wanted to connect to the server it shows
pgadmin connection refuse
Then I wrote host all all all md5 in pg_hba.conf file and I wrote listen_addresses = '*' to postgresql.conf file. and I wrote the following command sudo service postgresql restart but again It shows connection refuse message.
I wrote psql and it shows connections on Unix domain socket
Can you please help me in this regard? What I have missed?

Unable to connect to PostgreSQL on remote server via Pgadmin 4

I installed Ubuntu 14.04 on Azure recently. Have been trying to connect to PostgreSQL but the server refuses the connection. I checked and confirmed that it was online. I also tried changing the settings to trust on pg_hba.conf and I also edited the Postgresql.conf file to listen to all addresses. Furthermore, I checked my firewall settings on Windows and allowed Pgadmin 4 to go through. Despite following all the instructions in this question (Unable to connect PostgreSQL to remote database using pgAdmin), I was unable to connect. What should I do?
I once had such issue with pgAdmin4 on win 10. Here is the step I took to connect to my remote server
first enable port 5432 to pass through firewall in ubuntu:
sudo ufw allow 5432/tcp
Then edit your postgresql.conf file and add
listen_addresses = "*"
file can be found at /etc/postgresql//main/postgresql.conf
Proceed to edit pg_hba.conf and add
host all all 0.0.0.0/0 md5
Now stop the server using/etc/init.d/postgresql stop and restart /etc/init.d/postgresql start
You should be able to connect now. However, you can allow pgAdmin4 to pass through the windows firewall
control panel > System and Security > Allow an app through windows firewall
You can also allow same app for any antivirus you've installed
Note:
If you still cannot connect, you can reset your postgres user's password NOT linux default user
sudo -u postgres psql postgres
# \password postgres
Enter new password
Then use this new password to connect your pgAdmin4 using
postgres as Maintenance database
postgres as username
then new password
Hopefully, you should be able to connect
Enable your postgresql server to start at boot
sudo systemctl enable postgresql
Start your postgresql server
sudo systemctl start postgresql
verify your postgresql server is running:
sudo systemctl status postgresql

Postgresql connection via terminal - pgadmin

I have a problem connecting (using SSH) to my virtualbox(ubuntu) postgres server.
I set up all nice and correctly.
PostgreSQL settings:
postgresql.conf -> Allowed all incomming connection (*) and set ssl TRUE
pg_hba.conf -> after a lot of changes i came to the point where it seems to work with this settings:
IP4
host all all 0.0.0.0/0 trust
hostssl all all 0.0.0.0/0 trust
host all all 192.168.x.x/32 trust
hostssl all all 192.168.x.x/32 trust
I tried:
Made restart of my postgres server on the virtual machine.
Then i went to my host machine (Snow Leopard), build a ssh connection to the virtualbox (ubuntu) and it's working.
ping my guest machine on port 5432 also works.
Open pgadmin on my host machine (Snow Leopard) -> added server 192.68.56.1 and database pluto, user pippo. The connection worked and i see the DB.
Tried open TERMINAL in Snow Leopard and executed following cmd:
psql -h 192.168.56.1 -U pippo -d pluto
with ERROR:
psql: FATAL: no pg_hba.conf entry for host "192.168.56.1",
user "pippo", database "pluto", SSL off
I also tried to connect through my Java Program ans I got the same error.
What am I doing wrong?
pgAdmin tries to connect with and without SSL by default.
I suspect that you only try to connect without SSL via psql, while the server seems to require SSL for connections. Try:
psql "sslmode=require host=192.168.56.1 dbname=pluto" pippo
More about sslmode in the manual.
Open my query into pgAdmin from command line:
-f The file containing your SQL script to be loaded in the query window
-qc The connection string without password (you can also use ssl connections)
"C:\Program Files\PostgreSQL\9.4\bin\pgAdmin3.exe" -f "C:\slqFiles\FindFunctionByName.sql" -qc "host=localhost port=5432 dbname=myDatabase user=postgres"
The password will be collected from the password file located in the application folder: %APPDATA%\postgresql\pgpass.conf
You need permissions for connection. Try to add to C:\Program Files\PostgreSQL\9.6\data\pg_hba.conf next line:
host all all ::/0 trust