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)

  • Does this remain the same when you use SSL in the JDBC connection, too? Commented Nov 18, 2016 at 14:39
  • I got a few ssl related errors, but including ssl=true sslmode=verify-all sslrootcert=/path/to/rds-combined-ca-bundle.pem" gets back to the same "Connection Refused" error. Not specifying any SSL seems to be OK, especially for psql` (see output in question). Still tracing it down - I think its just my JAR.
    – clay
    Commented Nov 18, 2016 at 14:53
  • As you see, using psql you are connected over SSL - supposedly, the PGSSLMODE env variable is set correctly. OTOH, have you tried to specify a user in the connstring? Commented Nov 18, 2016 at 14:59

1 Answer 1


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.