title: Local PostgreSQL refusing connection with IntelliJ IDEA
body: <p>A week ago I installed PostgreSQL with Homebrew following <a href="http://www.moncefbelyamani.com/how-to-install-postgresql-on-a-mac-with-homebrew-and-lunchy/" rel="nofollow">this tutorial</a>. Everything worked fined and I was perfectly able to connect to the database in my Java Example Project. Two days ago, I've tried to see the code again and Java is throwing the following error:</p>
  
  <pre><code>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.&lt;init&gt;(AbstractJdbc2Connection.java:127)
      at org.postgresql.jdbc3.AbstractJdbc3Connection.&lt;init&gt;(AbstractJdbc3Connection.java:29)
      at org.postgresql.jdbc3g.AbstractJdbc3gConnection.&lt;init&gt;(AbstractJdbc3gConnection.java:21)
      at org.postgresql.jdbc4.AbstractJdbc4Connection.&lt;init&gt;(AbstractJdbc4Connection.java:41)
      at org.postgresql.jdbc4.Jdbc4Connection.&lt;init&gt;(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.&lt;init&gt;(PGStream.java:61)
      at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:121)
      ... 16 more
  </code></pre>
  
  <p>At the beginning I've thought postgresql just stopped listening the port, but it is listening:</p>
  
  <pre><code>$ 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)
  </code></pre>
  
  <p>And also I can connect from both PGAdmin and PSQL:</p>
  
  <pre><code>$ psql -d prakt -U barbarity
  psql (9.4.2)
  Type "help" for help.
  
  prakt=#
  </code></pre>
  
  <p>Then I tried to uninstall and reinstall from homebrew, but it didn't work neither. I've also tried Postgress.app and nothing also.</p>
  
  <p>Do you have any ideas?</p>
  
  <p>I'm using:</p>
  
  <ul>
  <li>Java SDK 1.8</li>
  <li>JDBC 41</li>
  <li>PostgreSQL 9.4.2</li>
  <li>IntelliJ IDEA 14.1.3</li>
  </ul>
  
  <p>(I put the java code just in case, but it was working a week ago)</p>
  
  <pre><code>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();
          }
      }
  }
  </code></pre>