2016-06-21 6 views
3

Ich versuche eine Verbindung zu einer Postgres-Datenbank herzustellen, die auf einem Amazon EC2-Server läuft. Ich weiß, dass der Server richtig konfiguriert ist, da ich von pgadmin auf ihn zugreifen kann und als Datenquelle innerhalb von intellij.Der Versuch, eine Postgres-Datenbank mit jsch und jdbc zu verbinden, ergibt "Der Verbindungsversuch ist fehlgeschlagen. Verursacht von: java.io.EOFException"

Ich kann auch mit der Server-Shell aus meinem Programm verbinden, wenn nur mit jsch und Verbindung zu einer anderen Datenbank direkt mit jdbc, aber sobald ich versuche, Verbindung zu meinem Postgres-Instanz von SSH-Tunneling mit JSCH Ich bin das seltsam gegeben Fehler, die ich nicht in der Lage gewesen, zu diagnostizieren .:

org.postgresql.util.PSQLException: The connection attempt failed. 
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:262) 
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:67) 
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:216) 
at org.postgresql.Driver.makeConnection(Driver.java:406) 
at org.postgresql.Driver.connect(Driver.java:274) 
at java.sql.DriverManager.getConnection(DriverManager.java:664) 
at java.sql.DriverManager.getConnection(DriverManager.java:208) 
at EnumStreamer.main(EnumStreamer.java:38) 
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 
Caused by: java.io.EOFException 
at org.postgresql.core.PGStream.ReceiveChar(PGStream.java:285) 
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:411) 
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:208) 
... 12 more 

Der Code I folgt verbinden bin mit:

System.out.println("Connecting..."); 
     session = secConnect.getSesssion(); 
     UserInfo ui = new uInfo(); 
     session.setUserInfo(ui); 
     session.connect(); 
     session.setPortForwardingL(5432,secConnect.getHost(),5432); 
     System.out.println("SSH Tunneled"); 

     System.out.println("Opening Database Connection"); 
     Class.forName("org.postgresql.Driver"); 
     Properties props = new Properties(); 
     props.setProperty("user","username"); 
     props.setProperty("password","password"); 
     props.setProperty("loglevel", "2"); 
     connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/dbname",props); 
     System.out.println("Connected"); 

Die entsprechenden Code des secConnect Objekt ist hier:

jsch.addIdentity(chooser.getSelectedFile().getAbsolutePath()); 

public Session getSesssion() throws JSchException { 
    return jsch.getSession(user, host, 22); 
} 

ist mein EC2 Postgres-Server mit konfiguriert listen = '*' und pg_hba.conf hat die Linie

host all all 0.0.0.0/0 md5 

DB Protokoll, wenn dies zu conect versucht erzeugt:

DEBUG: StartTransaction 
DEBUG: name: unnamed; blockState:  DEFAULT; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: 
DEBUG: CommitTransaction 
DEBUG: name: unnamed; blockState:  STARTED; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: 
DEBUG: InitPostgres 
DEBUG: my backend ID is 2 
DEBUG: StartTransaction 
DEBUG: name: unnamed; blockState:  DEFAULT; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: 
DEBUG: CommitTransaction 
DEBUG: name: unnamed; blockState:  STARTED; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: 
DEBUG: autovacuum: processing database "postgres" 
DEBUG: StartTransaction 
DEBUG: name: unnamed; blockState:  DEFAULT; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: 
DEBUG: pg_statistic: vac: 0 (threshold 127), anl: 0 (threshold 88) 
DEBUG: pg_type: vac: 0 (threshold 116), anl: 0 (threshold 83) 
DEBUG: pg_authid: vac: 7 (threshold 50), anl: 8 (threshold 50) 
DEBUG: pg_proc: vac: 0 (threshold 548), anl: 8 (threshold 299) 
DEBUG: pg_class: vac: 0 (threshold 108), anl: 0 (threshold 79) 
DEBUG: pg_attribute: vac: 0 (threshold 499), anl: 0 (threshold 275) 
DEBUG: pg_constraint: vac: 0 (threshold 50), anl: 0 (threshold 50) 
DEBUG: pg_operator: vac: 0 (threshold 196), anl: 0 (threshold 123) 
DEBUG: pg_opclass: vac: 0 (threshold 74), anl: 0 (threshold 62) 
DEBUG: pg_am: vac: 0 (threshold 51), anl: 0 (threshold 50) 
DEBUG: pg_amop: vac: 0 (threshold 129), anl: 0 (threshold 89) 
DEBUG: pg_amproc: vac: 0 (threshold 107), anl: 0 (threshold 79) 
DEBUG: pg_language: vac: 0 (threshold 51), anl: 0 (threshold 50) 
DEBUG: pg_aggregate: vac: 0 (threshold 74), anl: 0 (threshold 62) 
DEBUG: pg_rewrite: vac: 0 (threshold 71), anl: 0 (threshold 61) 
DEBUG: pg_trigger: vac: 0 (threshold 50), anl: 0 (threshold 50) 
DEBUG: pg_description: vac: 0 (threshold 740), anl: 1 (threshold 395) 
DEBUG: pg_database: vac: 0 (threshold 50), anl: 1 (threshold 50) 
DEBUG: pg_namespace: vac: 0 (threshold 51), anl: 0 (threshold 51) 
DEBUG: pg_depend: vac: 0 (threshold 1291), anl: 8 (threshold 670) 
DEBUG: pg_db_role_setting: vac: 0 (threshold 50), anl: 0 (threshold 50) 
DEBUG: pg_tablespace: vac: 0 (threshold 50), anl: 0 (threshold 50) 
DEBUG: pg_shdepend: vac: 0 (threshold 50), anl: 0 (threshold 50) 
DEBUG: pg_shdescription: vac: 0 (threshold 50), anl: 0 (threshold 50) 
DEBUG: pg_ts_config: vac: 0 (threshold 53), anl: 0 (threshold 52) 
DEBUG: pg_ts_dict: vac: 0 (threshold 53), anl: 0 (threshold 52) 
DEBUG: pg_ts_parser: vac: 0 (threshold 50), anl: 0 (threshold 50) 
DEBUG: pg_ts_template: vac: 0 (threshold 51), anl: 0 (threshold 50) 
DEBUG: pg_extension: vac: 0 (threshold 50), anl: 1 (threshold 50) 
DEBUG: pg_index: vac: 0 (threshold 72), anl: 0 (threshold 61) 
DEBUG: pg_cast: vac: 0 (threshold 89), anl: 0 (threshold 70) 
DEBUG: pg_toast_2619: vac: 0 (threshold 52), anl: 0 (threshold 51) 
DEBUG: pg_toast_2618: vac: 0 (threshold 84), anl: 0 (threshold 67) 
DEBUG: CommitTransaction 
DEBUG: name: unnamed; blockState:  STARTED; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: 
DEBUG: shmem_exit(0): 8 callbacks to make 
DEBUG: proc_exit(0): 2 callbacks to make 
DEBUG: exit(0) 
DEBUG: shmem_exit(-1): 0 callbacks to make 
DEBUG: proc_exit(-1): 0 callbacks to make 
DEBUG: reaping dead processes 
DEBUG: server process (PID 21628) exited with exit code 0 
DEBUG: StartTransaction 
DEBUG: name: unnamed; blockState:  DEFAULT; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: 
DEBUG: CommitTransaction 
DEBUG: name: unnamed; blockState:  STARTED; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: 
DEBUG: InitPostgres 
DEBUG: my backend ID is 2 
DEBUG: StartTransaction 
DEBUG: name: unnamed; blockState:  DEFAULT; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: 
DEBUG: CommitTransaction 
DEBUG: name: unnamed; blockState:  STARTED; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: 
DEBUG: autovacuum: processing database "patientSim" 
DEBUG: StartTransaction 
DEBUG: name: unnamed; blockState:  DEFAULT; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: 
DEBUG: pg_statistic: vac: 0 (threshold 127), anl: 0 (threshold 88) 
DEBUG: pg_type: vac: 5 (threshold 116), anl: 14 (threshold 83) 
DEBUG: pg_authid: vac: 7 (threshold 50), anl: 8 (threshold 50) 
DEBUG: pg_proc: vac: 0 (threshold 548), anl: 0 (threshold 299) 
DEBUG: pg_class: vac: 7 (threshold 108), anl: 20 (threshold 79) 
DEBUG: pg_attribute: vac: 36 (threshold 499), anl: 111 (threshold 275) 
DEBUG: pg_constraint: vac: 2 (threshold 50), anl: 5 (threshold 50) 
DEBUG: pg_operator: vac: 0 (threshold 196), anl: 0 (threshold 123) 
DEBUG: pg_opclass: vac: 0 (threshold 74), anl: 0 (threshold 62) 
DEBUG: pg_am: vac: 0 (threshold 51), anl: 0 (threshold 50) 
DEBUG: pg_amop: vac: 0 (threshold 129), anl: 0 (threshold 89) 
DEBUG: pg_amproc: vac: 0 (threshold 107), anl: 0 (threshold 79) 
DEBUG: pg_language: vac: 0 (threshold 51), anl: 0 (threshold 50) 
DEBUG: pg_aggregate: vac: 0 (threshold 74), anl: 0 (threshold 62) 
DEBUG: pg_rewrite: vac: 0 (threshold 71), anl: 0 (threshold 61) 
DEBUG: pg_trigger: vac: 0 (threshold 50), anl: 0 (threshold 50) 
DEBUG: pg_description: vac: 0 (threshold 740), anl: 0 (threshold 395) 
DEBUG: pg_database: vac: 0 (threshold 50), anl: 1 (threshold 50) 
DEBUG: config: vac: 10 (threshold 50), anl: 19 (threshold 50) 
DEBUG: pg_namespace: vac: 0 (threshold 51), anl: 0 (threshold 51) 
DEBUG: pg_depend: vac: 16 (threshold 1291), anl: 46 (threshold 670) 
DEBUG: pg_db_role_setting: vac: 0 (threshold 50), anl: 0 (threshold 50) 
DEBUG: pg_tablespace: vac: 0 (threshold 50), anl: 0 (threshold 50) 
DEBUG: pg_shdepend: vac: 0 (threshold 50), anl: 0 (threshold 50) 
DEBUG: pg_shdescription: vac: 0 (threshold 50), anl: 0 (threshold 50) 
DEBUG: pg_ts_config: vac: 0 (threshold 53), anl: 0 (threshold 52) 
DEBUG: pg_ts_dict: vac: 0 (threshold 53), anl: 0 (threshold 52) 
DEBUG: pg_ts_parser: vac: 0 (threshold 50), anl: 0 (threshold 50) 
DEBUG: pg_ts_template: vac: 0 (threshold 51), anl: 0 (threshold 50) 
DEBUG: pg_extension: vac: 0 (threshold 50), anl: 0 (threshold 50) 
DEBUG: pg_collation: vac: 0 (threshold 234), anl: 0 (threshold 142) 
DEBUG: pg_attrdef: vac: 1 (threshold 50), anl: 3 (threshold 50) 
DEBUG: pg_index: vac: 2 (threshold 72), anl: 6 (threshold 61) 
DEBUG: pg_cast: vac: 0 (threshold 89), anl: 0 (threshold 70) 
DEBUG: pg_toast_2619: vac: 0 (threshold 52), anl: 0 (threshold 51) 
DEBUG: pg_toast_2618: vac: 0 (threshold 84), anl: 0 (threshold 67) 
DEBUG: pg_toast_16409: vac: 0 (threshold 50), anl: 148 (threshold 50) 
DEBUG: CommitTransaction 
DEBUG: name: unnamed; blockState:  STARTED; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: 
DEBUG: shmem_exit(0): 8 callbacks to make 
DEBUG: proc_exit(0): 2 callbacks to make 
DEBUG: exit(0) 
DEBUG: shmem_exit(-1): 0 callbacks to make 
DEBUG: proc_exit(-1): 0 callbacks to make 
DEBUG: reaping dead processes 
DEBUG: server process (PID 21659) exited with exit code 0 

Antwort

2

Was das ist Ergebnis von secConnect.getHost()? Ist es die öffentliche Adresse? Wenn Sie direkt mit dem Server verbunden sind, die Datenbank ausgeführt wird vielleicht versuchen:

session.setPortForwardingL(5432, "127.0.0.1", 5432); 
+0

ich die IP des EC2-Server wurde mit, jedoch durch Substitution, dass mit der vorgeschlagenen Adresse scheint das Problem behoben zu haben. Vielen Dank! –

+1

@ YashSharma der Prozess auf dem * Server * verbindet sich mit der Remote-IP, so ist es, als ob Sie auf dem Remote-Server sind, z. 'psql -h remote_ip'. Sie würden 'psql -h ec2-public-ip' nicht auf dem Server ausführen, also legen Sie die öffentliche IP-Adresse nicht dort ab. – lmz

Verwandte Themen