I setup Postgresql as database for Django and it works as a charm. However, today I got a error message when I tried to migrate my django.

1
2
3
4
5
6
7
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: 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?

And sudo lsof -i :5432 returned nothing. It looks that my local database is down, so I tried brew services start postgresql@10, and got message:

1
Service `postgresql@10` already started, use `brew services restart postgresql@10` to restart.

I tried to restart postgresql, but it did not help.

Maybe there is a stuck lockfile somewhere? Here is a clue as well as a few workarounds. I examine the content of postmaster.pid by cat /usr/local/var/postgresql@10/postmaster.pid and kill the lock file by kill -9 <PID>. Upon restarting the services, now it works again.

p.s. kill -9 might not be a good way to kill the process here. See the quote here:

No no no. Don’t use kill -9. It doesn’t give the process a chance to cleanly:

  1. shut down socket connections
  2. clean up temp files
  3. inform its children that it is going away
  4. reset its terminal characteristics