本地PostgreSQL拒绝与IntelliJ IDEA连接

时间:2015-06-02 10:37:43

标签: java macos postgresql jdbc intellij-idea

一周前,我在this tutorial之后用Homebrew安装了PostgreSQL。所有工作都被罚款,我完全能够连接到我的Java示例项目中的数据库。两天前,我试图再次看到代码,Java正在抛出以下错误:

org.postgresql.util.PSQLException: Connection to localhost: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:239)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:127)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
    at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
    at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:41)
    at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:414)
    at org.postgresql.Driver.connect(Driver.java:282)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.company.Main.main(Main.java:16)
    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:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.postgresql.core.PGStream.<init>(PGStream.java:61)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:121)
    ... 16 more

一开始我以为postgresql刚停止收听端口,但它正在听:

$ lsof -i :5432
COMMAND    PID      USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
postgres 39225 barbarity    5u  IPv6 0x4ef6e84c8de72f23      0t0  TCP localhost:postgresql (LISTEN)
postgres 39225 barbarity    6u  IPv4 0x4ef6e84ca3a79493      0t0  TCP localhost:postgresql (LISTEN)

我也可以从PGAdmin和PSQL连接:

$ psql -d prakt -U barbarity
psql (9.4.2)
Type "help" for help.

prakt=#

然后我尝试从自制程序中卸载并重新安装,但它既没有工作也没有。我也尝试了Postgress.app,也没有尝试过。

你有什么想法吗?

我正在使用:

  • Java SDK 1.8
  • JDBC 41
  • PostgreSQL 9.4.2
  • IntelliJ IDEA 14.1.3

(我把java代码放在以防万一,但它在一周前工作了)

import java.sql.*;

public class Main {
    public static void main(String[] args) {
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String query = new StringBuilder()
                .append("SELECT * FROM customer WHERE id_customer = ? or f_name = ?").toString();

        try (Connection connection = DriverManager.getConnection("jdbc:postgresql:prakt", "barbarity", "");
             PreparedStatement preparedStatement = connection.prepareStatement(query);){

            preparedStatement.setInt(1, 1);
            preparedStatement.setString(2,"Richard");

            try (ResultSet resultSet = preparedStatement.executeQuery();){
                while (resultSet.next()) {
                    System.out.println(new StringBuilder()
                            .append("Name: ")
                            .append(resultSet.getString(2))
                            .toString());
                }
                resultSet.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案