无法使用JDBC连接到PostgreSQL Server 10

时间:2018-09-06 12:05:27

标签: postgresql jdbc postgresql-10

我可以与pgAdmin连接,但不能与JDBC连接,我在做什么错了?

enter image description here

JDBC:

方法1

String url = "jdbc:postgresql://localhost:5432/microlms360";

Properties props = new Properties();
props.setProperty("user","microlms360app");
props.setProperty("password","p0stgr@s");
props.setProperty("ssl","true");
Connection conn = DriverManager.getConnection(url, props);

方法2

String url = "jdbc:postgresql://localhost:5432/microlms360?user=microlms360app&password=p0stgr@s&ssl=false";
try (Connection conn = DriverManager.getConnection(url)) {
    boolean res = conn.createStatement().execute("SELECT 1");
    System.out.println(res);
}

两种情况下的结果都相同(我尝试使用localhost127.0.0.1作为主机名。都是徒劳的。)

Connected to the target VM, address: '127.0.0.1:8648', transport: 'socket'
Exception in thread "main" org.postgresql.util.PSQLException: Connection to 127.0.0.1:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:280)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
    at org.postgresql.Driver.makeConnection(Driver.java:454)
    at org.postgresql.Driver.connect(Driver.java:256)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:208)
    at iex.Main.main(Main.java:16)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.postgresql.core.PGStream.<init>(PGStream.java:70)
    at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
    ... 7 more
Disconnected from the target VM, address: '127.0.0.1:8648', transport: 'socket'

根据此postgresql.org blog,我正在使用最新的驱动程序

<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>42.2.5</version>
</dependency>

UPD:后端进程实际上是在侦听,因此postgress process is actually listening, so "nothing listen the port" is not the case

并非如此:

1 个答案:

答案 0 :(得分:1)

在正在运行Java应用程序的计算机上的localhost端口5432上没有监听任何内容,或者它被防火墙主动阻止。错误消息“ java.net.ConnectException:连接被拒绝:connect” 来自Java的低级套接字,毫无疑问它无法连接。

PgAdmin4未被防火墙阻止,或者您从PgAdmin4截取的屏幕截图来自另一台计算机,或者您正在连接到在另一台主机上运行的PgAdmin4,其中PostgreSQL在本地主机端口5432上运行。