title: "The connection attempt failed" Postgres at AWS Lambda
body: <p>I have a Postgres DB on AWS that is available outside. The connection works fine when I launch my app locally.</p>
  
  <p>When I launch it on AWS lambda it gives me this error:</p>
  
  <pre><code>org.postgresql.util.PSQLException: The connection attempt failed.
      at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292)
      at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
      at org.postgresql.jdbc.PgConnection.&lt;init&gt;(PgConnection.java:195)
      at org.postgresql.Driver.makeConnection(Driver.java:454)
      at org.postgresql.Driver.connect(Driver.java:256)
      at java.sql.DriverManager.getConnection(DriverManager.java:664)
      at java.sql.DriverManager.getConnection(DriverManager.java:270)
      at ninja.majewski.AmazonDbService.getRemoteConnection(AmazonDbService.java:84)
      at ninja.majewski.AmazonDbService.removeOldDeals(AmazonDbService.java:63)
      at ninja.majewski.ChinaStuffLambdaHandler.handleRequest(ChinaStuffLambdaHandler.java:19)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at lambdainternal.EventHandlerLoader$PojoMethodRequestHandler.handleRequest(EventHandlerLoader.java:259)
      at lambdainternal.EventHandlerLoader$PojoHandlerAsStreamHandler.handleRequest(EventHandlerLoader.java:178)
      at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888)
      at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:292)
      at lambdainternal.AWSLambda.&lt;clinit&gt;(AWSLambda.java:64)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:348)
      at lambdainternal.LambdaRTEn
      try.main(LambdaRTEntry.java:94)
  </code></pre>
  
  <p>This is the code that fails:</p>
  
  <pre><code>private Connection getRemoteConnection() throws Exception {
      Class.forName("org.postgresql.Driver");
      String dbName = System.getenv("RDS_DB_NAME");
      String userName = System.getenv("RDS_USERNAME");
      String password = System.getenv("RDS_PASSWORD");
      String hostname = System.getenv("RDS_HOSTNAME");
      String port = System.getenv("RDS_PORT");
      String jdbcUrl = "jdbc:postgresql://" + hostname + ":" + port + "/" + dbName + "?user=" + userName + "&amp;password=" + password;
      log.info(String.format("Logging to DB: %s", jdbcUrl));
      return DriverManager.getConnection(jdbcUrl);
  }
  </code></pre>
  
  <p>What could be the reason for this?</p>