title: PostgreSQL: Could not connect to server: TCP/IP connections on port 5432
body: <p>I have an error with PostgreSQL:</p>
  
  <blockquote>
    <p>SQLSTATE[08006] [7]  could not connect to server: Connection refused
            Is the server running on host "localhost" (::1) and 
            accepting TCP/IP connections on port 5432? could not connect to server: Connection refused 
            Is the server running on host "localhost" (127.0.0.1) and  accepting TCP/IP connections on port 5432?</p>
  </blockquote>
  
  <p>The following command doesn't show 5432 port is open:</p>
  
  <pre><code>sudo netstat -plunt |grep postgres
  </code></pre>
  
  <p>I guess that the problem is 5432 port so as I don't see it at <code>netstat</code>.<br/> How to open port for PostgreSQL?</p>
  
  <ol>
  <li>The <code>postgresql.conf</code> and <code>pg_hba.conf</code> files are okay (see below).</li>
  <li><code>postgres</code> server is running</li>
  <li>My <code>postgresql.conf</code> file:</li>
  </ol>
  
  <pre class="lang-sql prettyprint-override"><code>listen_addresses = '*'
  port = 5432
  </code></pre>
  
  <ol start="4">
  <li>My <code>pg_hba.conf</code> file:</li>
  </ol>
  
  <pre class="lang-sql prettyprint-override"><code>local   all             postgres                                md5
  local   all             all                                     md5
  host    all             all             0.0.0.0/0               trust
  host    all             all             127.0.0.1/32            trust
  host    all             all             ::1/128                 trust
  local   replication     all                                     peer
  host    replication     all             127.0.0.1/32            md5
  host    replication     all             ::1/128                 md5
  </code></pre>
  
  <ol start="3">
  <li>I ran the command: <code>$ netstat -atu</code></li>
  </ol>
  
  <pre><code>Active Internet connections (servers and established)
  Proto Recv-Q Send-Q Local Address           Foreign Address         State
  tcp        0      0 localhost:6379          0.0.0.0:*               LISTEN
  tcp        0      0 0.0.0.0:http            0.0.0.0:*               LISTEN
  tcp        0      0 localhost:domain        0.0.0.0:*               LISTEN
  tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN
  tcp        0      0 0.0.0.0:https           0.0.0.0:*               LISTEN
  tcp        0      0 30secondboomer.com:ssh  109-252-90-59.nat:11807 ESTABLISHED
  tcp        0      0 30secondboomer.com:ssh  109-252-90-59.nat:11258 ESTABLISHED
  tcp        0      0 30secondboomer.com:ssh  109-252-90-59.nat:11797 ESTABLISHED
  tcp6       0      0 localhost:6379          [::]:*                  LISTEN
  tcp6       0      0 [::]:http               [::]:*                  LISTEN
  tcp6       0      0 [::]:ssh                [::]:*                  LISTEN
  tcp6       0      0 [::]:https              [::]:*                  LISTEN
  udp        0      0 localhost:domain        0.0.0.0:*
  udp        0      0 30secondboomer.c:bootpc 0.0.0.0:*
  </code></pre>