Local PostgreSQL refuses to connect to IntelliJ IDEA

A week ago I installed PostgreSQL with Homebrew following this tutorial . Everything worked fine and I could connect to the database perfectly fine in my Java Example Project. Two days ago I tried to see the code again and Java throws the following error:

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

      

In the beginning I thought that postgresql just stops listening on the port, but it is listening:

$ 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)

      

And also I can connect from both PGAdmin and PSQL:

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

prakt=#

      

Then I tried uninstalling and reinstalling with homebrew, but that didn't work. I've also tried Postgress.app and nothing.

Do you have any ideas?

I use:

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

(I pasted the Java code just in case, but it worked a week ago)

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();
        }
    }
}

      

+3


source to share





All Articles