title: PostgreSQL Connection Timed Out OperationalError on Django and new server
body: <p>After my server got compromised, I've had to migrate to a new droplet - both on DigitalOcean, Debian Jessie, so I shouldn't have the Heroku issues that a lot of the similar questions seem to cover.</p>
  
  <p>I copied the database over using pg_dump, and it seems intact. PostgreSQL is installed, and running, but whenever I try to utilise the database from the Django ORM, I get the error -</p>
  
  <pre><code>OperationalError: could not connect to server: Connection timed out
  Is the server running on host "localhost" (127.0.0.1) and accepting
  TCP/IP connections on port 5432?
  </code></pre>
  
  <p>So, some obvious things first.  </p>
  
  <p>Netstat shows:</p>
  
  <pre><code>tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      613/postgres    
  tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      554/redis-server 12
  </code></pre>
  
  <p>I can see several PostgreSQL process in <code>top</code> and using <code>ps auxw | grep postgres</code>:</p>
  
  <pre><code> 1072 postgres  20   0  234680 129672 126676 S   6.0  3.2   3:06.98 postgres: me databasename [local] COPY                                          +
    640 postgres  20   0  227000 119564 117876 S   0.3  2.9   0:03.77 postgres: checkpointer process                                                  +
    613 postgres  20   0  226712  21252  19824 S   0.0  0.5   0:00.05 /usr/lib/postgresql/9.4/bin/postgres -D /var/lib/postgresql/9.4/main -c config_f+
    641 postgres  20   0  226712   5556   4080 S   0.0  0.1   0:02.59 postgres: writer process  
  </code></pre>
  
  <p><code>systemctl</code> shows</p>
  
  <pre><code>postgresql.service                   loaded active exited 
  postgresql@9.4-main.service          loaded active running   
  </code></pre>
  
  <p>psycopg2 is installed, and I'm working from inside the virtualenv.  </p>
  
  <p>Any queries I run from shell using the Django ORM return the above error. I can access and run queries from psql, and it returns data fine, so PostgreSQL appears to be running, but Django can't reach it for whatever reason. The password and username are correct, although I think that would throw a different error in anycase. <a href="https://stackoverflow.com/questions/29636574/django-postgres-could-not-connect-to-server">Restarting the service</a> it makes no difference.</p>
  
  <p>My Django settings are:</p>
  
  <pre><code>DATABASES = {
  "default": {
      "ENGINE": "django.db.backends.postgresql_psycopg2",
      "NAME": "databasename",
      "USER": "databaseusername",
      "PASSWORD": "databasepasswordhere",
      "HOST": "localhost",
      "PORT": "5432",
      "CONN_MAX_AGE":360,
  }
  }
  </code></pre>
  
  <p>(I've tried both localhost and 127.0.0.1 as host, in case it was an IPv4/IPv6 resolution thing, albeit unlikely)
  <code>pip freeze</code> shows:</p>
  
  <pre><code>Django==1.9.6
  argparse==1.2.1
  click==6.6
  django-extensions==1.6.7
  django-redis==4.4.3
  django-redis-cache==1.6.5
  django-rq==0.9.1
  django-rq-dashboard==0.3.0
  gunicorn==19.5.0
  hiredis==0.2.0
  newrelic==2.64.0.48
  nltk==3.2.1
  oauthlib==1.1.1
  psycopg2==2.6.1
  requests==2.10.0
  requests-oauthlib==0.6.1
  rq==0.6.0
  six==1.10.0
  tweepy==3.5.0
  wsgiref==0.1.2
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/32805224/django-db-utils-operationalerror-could-not-connect-to-server-no-such-file-or-d">Per this question,</a> I've checked that PostgreSQL, PostgreSQL-contrib, libpq-dev Python-dev are installed.</p>
  
  <p>Testing the connection on command-line <a href="https://stackoverflow.com/questions/32098797/how-can-i-check-database-connection-to-mysql-in-django">as per this question</a> throws the same error.  </p>
  
  <p>The real kicker here is - this is all working on the old server, and I can run ORM queries just fine - but it's not a secure server anymore, package manager is broken, so I need to abandon it, and need the new one to work. I can't see any differences in configuration between the two - I wondered if there was a permissions issue, but I get the same issue running as root/via sudo as my regular user.   </p>
  
  <p>I'm sure there's a simple configuration setting I've missed, but I'm pretty stumped at the moment!</p>
  
  <p>Edit:</p>
  
  <p>Grabbing the PID of postgres main process and running lsof gives this, though: </p>
  
  <p><code>postgres 613 postgres    6u  IPv4              11589      0t0    TCP localhost:postgresql (LISTEN)
  postgres 613 postgres    7u  unix 0xffff8800d8e1cc00      0t0  11591 /var/run/postgresql/.s.PGSQL.5432</code> </p>
  
  <p>Which seems weird.  Is it defaulting to a Unix socket there?</p>