Home > Software design >  Can't connect from outside to Postgres running on AWS EC2 instance
Can't connect from outside to Postgres running on AWS EC2 instance

Time:11-24

I have installed Postgres 10 on my EC2 ubuntu(18). But I can't access it from my Local Mac. Get this error.

enter image description here

My SETUP is:

I updated Postgres configs:

  1. to /etc/postgresql/10/main/pg_hba.conf added these lines:
    host    all             all             0.0.0.0/0               md5
    host    all             all             ::/0                    md5
  1. in /etc/postgresql/10/main/postgresql.conf changed this line:
    listen_addresses = '*'                  # what IP address(es) to listen on;
  1. I did set password to default 'postgres' user, which I am using to connect to DB from outside;

On EC2 instance I changed:

  1. added "Inbound rule" for port 5432 from any IPv4: enter image description here

On EC2 instance this command returns that port is exposed:

ubuntu@ip-XXXXXXXXXX:~$ netstat -nat |grep :5432
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN     
tcp6       0      0 :::5432                 :::*                    LISTEN   

Looks like EC2 instance did not expose this port in fact. Other ports like :80 or :22 are accessible fine, but port :5432 returns error:

➜  ~ nc -zv XX.XXX.XXX.XXX 80  
Connection to XX.XXX.XXX.XXX port 80 [tcp/http] succeeded!

➜  ~ nc -zv XX.XXX.XXX.XXX 5432
nc: connectx to XX.XXX.XXX.XXX port 5432 (tcp) failed: Operation timed out

I also have Nginx installed on my EC2 instance, its config is:

server {
  charset utf-8;
  listen 80;
  server_name XXXXXXXXXXXXXXX.ca; # <--- hidden domain name
  location / {
    root /opt/frontend/develop/dist/tweeter-ui/;
    try_files $uri /index.html;
  }
  location /api/ {
    proxy_pass http://localhost:8080/api/;
  }
}

PostgreSQL service is running:

ubuntu@ip-XXXXXXXXXX:~$ service postgresql status
● postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
   Active: active (exited) since Wed 2022-11-23 22:41:33 UTC; 1h 6min ago
  Process: 6283 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
 Main PID: 6283 (code=exited, status=0/SUCCESS)

Nov 23 22:41:33 ip-XXXXXXXXXX systemd[1]: Starting PostgreSQL RDBMS...
Nov 23 22:41:33 ip-XXXXXXXXXX systemd[1]: Started PostgreSQL RDBMS.

What is wrong in my setup? I tried many different posts in this forum and on Internet, nothing helps. (( Do I have to also configure Nginx with Postgres routing?

CodePudding user response:

You followed the correct steps. The issue is that the firewall is blocking the port of Postgres, 5432, so need to add it to the firewall allowed list.

sudo ufw allow 5432/tcp

When you run $ sudo ufw status, you'll see:

Status: active

To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx Full ALLOW Anywhere
5432/tcp ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx Full (v6) ALLOW Anywhere (v6)
5432/tcp (v6) ALLOW Anywhere (v6)

And then run sudo firewall-cmd --reload, when you see success, you're done!

You will be able to connect from outside of the instance

  • Related