title: Cannot connect to PostgreSQL database as 'localhost'
body: <p>I am developing an Android application and I'm using PostgreSQL bundled with PostGIS. I want to use a local database for that but connecting via 'localhost' doesn't work, I have tried the alternative to use '127.0.0.1' instead but it is not working, I am getting the following error:</p>
  
  <pre><code>org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
  </code></pre>
  
  <p>Using my network's ipv4 adress works, but I cannot connect via localhost. How can I fix that?
  My pg_hba.conf looks like:</p>
  
  <pre><code>...
  # Put your actual configuration here
  # ----------------------------------
  #
  # If you want to allow non-local connections, you need to add more
  # "host" records.  In that case you will also need to make PostgreSQL
  # listen on a non-local interface via the listen_addresses
  # configuration parameter, or via the -i or -h command line switches.
  
  
  
  # TYPE  DATABASE        USER            ADDRESS                 METHOD
  
  # IPv4 local connections:
  host    all             all             127.0.0.1/32            trust
  host    all             all             192.168.1.1/24          trust
  host    all             all             0.0.0.0/0               trust
  # IPv6 local connections:
  host    all             all             ::1/128                 trust
  
  # Allow replication connections from localhost, by a user with the
  # replication privilege.
  #host    replication     postgres        127.0.0.1/32            trust
  #host    replication     postgres        ::1/128                 trust
  </code></pre>
  
  <p>and my postgresql.conf looks like:</p>
  
  <pre><code># - Connection Settings -
  
  listen_addresses = '*'      # what IP address(es) to listen on;
                      # comma-separated list of addresses;
                      # defaults to 'localhost'; use '*' for all
                      # (change requires restart)
  port = 5123         # (change requires restart)
  max_connections = 100           # (change requires restart)
  </code></pre>
  
  <p>My code in Eclipse looks like </p>
  
  <pre><code>private String DB_URL = "jdbc:postgresql://" + Server.getDbserver() + ":" + Server.getDbport() + "/postgis";
  </code></pre>
  
  <p>And my variables inside my Server class look like.</p>
  
  <pre><code>private static String dbserver = "localhost";
  private static int dbport = 5123;
  </code></pre>
  
  <p>As a summary, connecting via my network ipv4 (for example 192.168.1.199) works, but connecting via 'localhost' or '127.0.0.1' generates me the error I posted above.</p>