To close the port 5432 for any traffic or don’t want to configure PostgreSQL to listen to any remote traffic, use SSH Tunneling to make a remote connection to the PostgreSQL instance at AWS.
Follow these steps to connect PostgreSQL using SSH Tunneling at AWS:
- Open PuTTY. Setup server session in Putty.
- Go to Connection > SSH > Tunnels
- Enter 8000 in the Source Port field.
- Enter 127.0.0.1:5432 in the Destination field.
- Click the “Add” button.
- Go back to Session, and save, then click “Open” to connect.
- This opens a terminal window. After connection leaves that alone.
- Open pgAdmin and add a connection.
- Enter localhost in the Host field and 8000 in the Port field.
- Specify a Name for the connection, and the username and password. Click OK.
What is it doing? PuTTY is intercepting communications sent from pgAdmin to localhost:8000. The information is transferred across the internet via SSH, on port 22. When it arrives there, the SSH server sends the information on to PostgreSQL via port 5432. As far as PostgreSQL knows, the traffic came in locally, on the correct port.