Failed connection to postgres on EC2 only, works on local machine - postgresql

I'm running an application on an EC2 instance (AWS cloud computing solution) that connects to a specific Database address.
When I run the application in my local machine it connects perfectly to the DB.
However, when I run it in the EC2 the connection hangs indefinitely. I've also tried to connect using psql to prove that the error was on the connection and received the "Connection timed out error".
Both my local machine and the EC2 instance are running ubuntu 22.04.
Does anyone have any idea what could be going on?

Both RDS instance and EC2 instance need to be in the same VPC, check your inbound rule of security group of your RDS. It must allow security group of your EC2 instance.
If you configured it to connect through public internet (not recommended) like what you did from local, the inbound rule of RDS need to allow IP of your EC2 instance.

Related

Connecting Tableau to a RDS DB in private tier using web proxy

I have the following architecture in AWS where the RDS DB is publicly unavailable and is inside a private subnet and I am currently connecting to the database through a ec2 jump box located in the public subnet using ssh port forwarding.
To connect to RDS we have two options.
SSH port forwarding setup on EC2
Web Proxy on EC2
I am able to connect using the SSH port forwarding option but I am not able to use Tableau as there is no option in Tableau to connect using .pem file which is required for SSH port connection to go through.
I am trying to see if there is documentation that will explain step by step on the process for setting up web proxy on the EC2 so that I can connect Tableau Desktop with the Database using the Elastic IP of the EC2 instance. Any help appreciated.

is it possible to connect to AWS RDS db instance(postgres) through ubuntu server installed out of aws(in my local)?

Actually i have to migrate the data from AWS RDS postgres to on premises postgres.
For this i have to establish connectivity between my AWS RDS db instance and on premises(ubuntu server).
I have already configured the inbound rules of security group in my RDS DB instance. But still i am not able to connect to AWS RDS db instance directly through my Ubuntu Machine..
Is there any issue with the firewall of my VM??
Error which i am getting:
psql: error: could not connect to server: Connection timed out
Is the server running on host "XXXXXX" (X.X.X.X/X) and accepting
TCP/IP connections on port 5432?
The database setting for publicly accessible is set to NO.
There are both public as well as private subnet associated with my RDS instance.
i have provided the type as postgreSQL , port 5432 , protocol TCP, source custom ip (X.X.X.X/32)
Any help will be much appreciated.
To access an Amazon RDS database from the Internet:
Set the database to Publicly Accessible = Yes (This resolves the DNS name to a Public IP address)
Put the database in a Public Subnet (Mixing public & private subnets in a Subnet Group is not advisable, because the results will be random! It will only be in one subnet at any time, but might change subnets when upgraded or failover is triggered.)
Add a Security Group that allows Inbound access on the appropriate port (PostgreSQL = 5432) from your IP address
Reference the database via its DNS Name shown in the Amazon RDS management console
Alternatively, keep the database in a Private Subnet and access via a VPN Connection or by Port Forwarding via a Bastion Server.

connecting springboot application running locally to db instance running on aws VM instance

I have written a set of APIs using springboot framework. Locally its working fine. But now I am trying to test it with the postgresql database running on ec2 instance on aws.
NOTE: I am not using amazon RDS services, I have created an ec2 instance and installed postgres and configured database there.
I can see this exception being thrown when I am running my springboot application locally.
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:297)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:211)
This is the config from application.properties for one of the db
spring.xyz.datasource.jdbc-url=jdbc:postgresql://xyz.com:5432/xyz_db
spring.xyz.datasource.username=xyz
spring.xyz.datasource.password=xyz123
Am I not being able to connect because of the EC2 instance security configs ? How can I change that ?
Also it will be helpful if someone can tell me how can I make aws ec2 instance to accept the connection from only one VM ?
regarding Postgress connection-
add below rule to security group of Ec2 instance , on which you have installed postGress
Allow inbound traffic to postgress port from 0.0.0.0/0 and test. ( protocol- TCP, port - 5432, IP - 0.0.0.0/0)
regarding allow from only one VM
add security group rule to your Ec2 instance
Allow inbound traffic from your VM IP only

Can't connect to RDS through EC2

I'm trying to access an RDS instance from an EC2 instance but with no luck. I'm able to connect to RDS from pgAdmin without any problem, but I'm try to connect to it from EC2 using psql I get the following error:
psql: could not translate host name "pgrds.cqvae2m3k8q9.us-west-2.rds.amazonaws.com" to address: Name or service not known
I have both instances running in the same VPC and using the same security group, which have all traffic open both ways. I've spent quite some time on this, and tried several solutions but none worked.

AWS - Connect to RDS via EC2 tunnel

My RDS is in a VPC, so it has a private IP address. I can connect my RDS database instance from my local computer with pgAdmin using SSH tunneling via EC2 Elastic IP.
Now I want to connect to the database instance in my code in python. How can I do that?
Point your python code to the same address and port you're using for the tunnelling.
If you're not sure check the pgAdmin destination in the configuration and just copy it.