title: Amazon RDS PostgreSQL "psql: error: could not connect to server: could not connect to server: Operation timed out"
body: <p>I’m at a loss for how to get connected to my Amazon PostgreSQL database instance. I’m sure I’m doing something wrong, and none of the other Stack Overflow threads have helped. This is an RDS DB Instance in a VPC Accessed by an EC2 Instance in the same VPC. Here's what I did.</p>
  <p><strong>Created a custom EC2 VPC security group, specified my default VPC</strong>:</p>
  <ul>
  <li><code>aws ec2 create-security-group --group-name rds-us-east-databases --vpc-id vpc-1234567 --description &quot;RDS databases in the us-east-1 region&quot;</code></li>
  </ul>
  <p><strong>Create an RDS database subnet group for my default subnets</strong>:</p>
  <ul>
  <li><code>aws rds create-db-subnet-group --db-subnet-group-name rds-us-east-databases --db-subnet-group-description &quot;RDS databases&quot; --subnet-ids '[&quot;subnet-97xxx9&quot;,&quot;subnet-e1xxxxdf&quot;,&quot;subnet-a62xxx9&quot;,&quot;subnet-baxxxxdc&quot;,&quot;subnet-axxxxe2&quot;,&quot;subnet-11xxxx0&quot;]'</code></li>
  </ul>
  <p><strong>Added CIDR traffic rules for my EC2 VPC security group</strong>**:</p>
  <ul>
  <li><code>aws ec2 authorize-security-group-ingress --group-name rds-us-east-databases --protocol tcp --port 5432 --cidr 160.3.6.253/32</code></li>
  <li><code>aws ec2 authorize-security-group-ingress --group-name rds-us-east-databases --protocol tcp --port 5432 --cidr 192.168.1.0/24</code></li>
  <li><code>aws ec2 authorize-security-group-ingress --group-name rds-us-east-databases --protocol tcp --port 5432 --cidr 10.0.0.0/16</code></li>
  </ul>
  <p>**Here I was testing traffic rules.</p>
  <p><strong>Created my PostgreSQL database and specified the EC2 VPC security group ID</strong>:</p>
  <ul>
  <li><code>aws rds create-db-instance --db-name DBName --db-instance-identifier MyDbNameID --allocated-storage 20 --db-instance-class db.t2.micro --engine Postgres --master-username postgres --master-user-password MyDatabasePassword1 --vpc-security-group-ids '[&quot;sg-02xxxxxxxxxxxx90c&quot;]' --db-subnet-group-name rds-us-east-databases --backup-retention-period 3 --port 5432 --no-publicly-accessible --enable-iam-database-authentication --region us-east-1 --max-allocated-storage 100</code></li>
  </ul>
  <p><strong>Tried to connect using psql</strong>.</p>
  <pre><code>psql --host=blah.blah.us-east-1.rds.amazonaws.com --port=5432 --username=postgres --password --dbname=DBName
  </code></pre>
  <p><strong>Error</strong>:</p>
  <pre><code>psql: error: could not connect to server: could not connect to server: Operation timed out
      Is the server running on host “blah.blah.rds.amazonaws.com&quot; (IP address) and accepting
      TCP/IP connections on port 5432?
  </code></pre>
  <p><strong>Note</strong>:</p>
  <ul>
  <li>All of the above is on/using the same VPC.</li>
  <li>I experienced the same issue a few months ago for another RDS PostgreSQL database and I was/am able to get connected. Though, I don't remember what I did to fix it. So the fault is not with the default VPC.</li>
  <li>Also confirmed that I have remote access setup in postgresql.conf, and since I can connect to the other RDS DB, doesn't seem like it would be a local PostgreSQL issue.</li>
  <li>Also confirmed it doesn't have anything to do with public accessibility or not. Tried making it publicly accessible and got a FATAL error, switched back.</li>
  </ul>
  <p>Other successful connection to RDS DB shown here:</p>
  <pre><code>psql --host=database-2.otherdbendpoint.us-east-1.rds.amazonaws.com --port=5432 --username=postgres --password --dbname=myotherdatabase
  Password: 
  psql (12.3, server 11.6)
  SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
  Type &quot;help&quot; for help.
  myotherdatabase=&gt;
  </code></pre>
  <p><strong>Update</strong>: all of the ingress rules for my VPC security group '[&quot;sg-02xxxxxxxxxxxx90c&quot;]':</p>
  <pre><code>PostgreSQL  TCP 5432    111.1.1.111/32  MY IP ADDRESS
  PostgreSQL  TCP 5432    192.168.1.0/24  -
  PostgreSQL  TCP 5432    0.0.0.0/0   -
  PostgreSQL  TCP 5432    10.0.0.0/16 -
  PostgreSQL  TCP 5432    10.0.0.0/24 -
  All traffic All All     5432::/16   -
  </code></pre>
  <p><strong>Update</strong>: my outgress rules for my VPC security group '[&quot;sg-02xxxxxxxxxxxx90c&quot;]':</p>
  <pre><code>All traffic All All 0.0.0.0/0   -
  </code></pre>