0
votes

I am trying to create the redshift connection using redshift jdbc driver which I downloaded from AWS redshift cluster console. Getting below exception

java.sql.SQLException: The connection attempt failed.
    at com.amazon.redshift.util.RedshiftException.getSQLException(RedshiftException.java:48)
    at com.amazon.redshift.Driver.connect(Driver.java:303)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
    at com.redshift.jdbc.test.ConnectionTest.main(ConnectionTest.java:27)
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.base/java.net.PlainSocketImpl.waitForConnect(Native Method)
    at java.base/java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:107)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
    at java.base/java.net.Socket.connect(Socket.java:591)
    at com.amazon.redshift.core.RedshiftStream.<init>(RedshiftStream.java:86)
    at com.amazon.redshift.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:101)
    at com.amazon.redshift.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:214)
    at com.amazon.redshift.core.ConnectionFactory.openConnection(ConnectionFactory.java:51)
    at com.amazon.redshift.jdbc.RedshiftConnectionImpl.<init>(RedshiftConnectionImpl.java:288)
    at com.amazon.redshift.Driver.makeConnection(Driver.java:466)
    at com.amazon.redshift.Driver.connect(Driver.java:279)
    ... 3 more

code :

String jdbcURL = "jdbc:redshift://host:5439/database";
    String user = "*****";
    String password = "****";
    
    String driver = "com.amazon.redshift.jdbc42.Driver";
    Connection connection = null;
    Statement statement = null;
    try {
        Class.forName(driver);
        connection = DriverManager.getConnection(jdbcURL, user, password);
        if (connection != null) {
            System.out.println("Connection created!");
            
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (connection != null)
                connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Can anyone please help me why I'm unable to make the connection?

1

1 Answers

1
votes

I guess Caused by: java.net.SocketTimeoutException: connect timed out is a key, here's a few possibilities:

  • your ip is not whitelisted in redshift security group
  • if the code above is exactly what you're running then String jdbcURL = "jdbc:redshift://host:5439/database"; doesn't contain a correct host and database, it myst something like your-cluster-name.blahblahblah.eu-central-1.redshift.amazonaws.com:5439/your-database-name you can find this jobs url in redshift console -> cluster general info

enter image description here


UPDATE


Check your IP whitelist in security group

enter image description here

and add your IP if it's not here enter image description here