Postgresql on AWS RDS JDBC Connection Refused, psql connection succeeeds

amazon-rdsjdbcpostgresql

I have an Amazon RDS Postgres instance running. I can see the data from my local machine (whitelist office IP) and our web app can connect (running on EC2 instance).

On that EC2 instance, these things happen:

[ec2-user@ip ~]$ nc -zv mydb.us-east-1.rds.amazonaws.com 5432
Connection to mydb.us-east-1.rds.amazonaws.com 5432 port [tcp/postgres] succeeded!


[ec2-user@ip ~]$ psql postgres -h mydb.us-east-1.rds.amazonaws.com -U userguy
Password for user userguy: 
psql (9.5.4, server 9.6.1)
WARNING: psql major version 9.5, server major version 9.6.
         Some psql features might not work.
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

postgres=> \q


[ec2-user@ip myjars]$ java -jar MyJar.jar param1
Connecting to jdbc:postgresql://mydb.us-east-1.rds.amazonaws.com:5432/postgres
org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

I know this EC2 instance can connect to the DB. The nc command works and the psql command works. We have a Glassfish application server that connects as well.

Same parameters are in this JAR file and it does not work! Any thoughts? Any ideas to diagnose? (as I type this, I feel it will be some file version or mistyped value in the JAR file, since everything else is working)

Best Answer

Ultimately the error was in the JAR. A connection was made the DB on two different occasions, and each one from a different config location. I changed one, but not the other - the joy of inheriting code : )

So now my DB connections works. Hopefully the original question has some useful ways to know if a connection can connect.