我试图在Windows 7和Ubuntu 14.04之间创建数据库连接。在这种情况下,我已经在我的Ubuntu系统上安装了Postgresql,并将'postgresql.conf'的listen_addresses从'localhost'更改为'*'。接下来,我将'host all all 192.168.2.1/24 md5'添加到'pg_hba.conf'文件中。
使用此配置,可以将pgAdmin 3连接到数据库。但是,如果我试图连接jdbc它不起作用。
我正在使用这些版本 - JDBC:'9.3-1103 JDBC 3',Postgresql:9.3。
首先,我使用基本代码构建了一个小测试项目:
Properties properties = new Properties();
properties.setProperty("user", "user");
properties.setProperty("password", "pw");
properties.setProperty("ssl", "true");
String url = "jdbc:postgresql://192.168.2.143:5432/testdb";
String driverJDBC = "org.postgresql.Driver";
Connection connection = null;
try {
Class.forName(driverJDBC);
connection = DriverManager.getConnection(url, properties);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.getStackTrace();
}
try {
if (connection != null) {
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("select * from testtable;");
while (rs.next()) {
System.out.println(rs.getString(2));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
这个小程序产生错误信息:
org.postgresql.util.PSQLException: Der Verbindungsversuch schlug fehl.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:240)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:143)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
at org.postgresql.jdbc3g.Jdbc3gConnection.<init>(Jdbc3gConnection.java:24)
at org.postgresql.Driver.makeConnection(Driver.java:412)
at org.postgresql.Driver.connect(Driver.java:280)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at db.DBConnection.make(DBConnection.java:23)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:75)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
我试过禁用Windows防火墙,其他JDBC版本(4)。
也许有人有想法?
聚苯乙烯。希望我的英语还可以;)
答案 0 :(得分:0)
Well 192.168.2.1/24
表示24位严格(24位是3个字节(24/8 = 3),因此前三个数字是严格的,1
可以不同),因此您需要一个客户端-来自192.168.2.XXX
的IP。
如果这不起作用,请尝试从日志记录中获取信息。
/etc/postgresql/9.4/main/postgresql.conf
。 (也许要备份)#log_destination = 'stderr
行,并将目标设置为syslog log_destination = 'syslog'
。tail -f /var/log/syslog | grep postgres
并尝试从客户端连接。您应该看到类似这样的内容:
Oct 7 11:02:18 Ubuntu-1510-wily-64-minimal postgres[1732]: [4-1] 2019-10-07 11:02:18 CEST [1732-2] postgres@cms LOG: connection authorized: user=XXX database=cms SSL enabled (protocol=TLSv1.2, cipher=ECDHE-RSA-AES128-GCM-SHA256, compression=off)