Postgresql – Postgres server not starting anymore

postgresqlUbuntu

My postgres was working previously, but for some reason does not work anymore. I have tried several different searches in stack overflow and other sites, and after coming up with the same results and reading the same pages over and over again, without any success, it was time to post a new question.

Trying to run my rails project on the local host raises the following error message: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

The server is not running at all, as indicated by the following output from ps -fu postgres:

postgres 23969 23968 0 09:54 pts/25 00:00:00 -bash

Error messages that I'm getting:

  • When running as the "postgres" user, executing psql results in:

psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

  • When trying to start postgres:

Other things I have tried:

  • Executing postgres -D /usr/local/pgsql/data >logfile 2>&1 & (as indicated by the official documentation) outputs: [1] 24670. But the server still doesn't run.

  • I also tried starting the server via service postgres start. This prompts me to my password to authenticate me as the user (it says Authentication is required to start 'postgres.service'.) My login succeeds (==== AUTHENTICATION COMPLETE ===)but then it says Failed to start postgres.service: Unit postgres.service not found.

  • I tried restarting the server (following these instructions using sudo /etc/init.d/postgresql restart), which in the past did fix postgres, but now simply prompts Restarting postgresql (via systemctl): postgresql.service. However, now I try to start the server again, and nothing has changed.

  • The problem does not seem to be any postmaster.pid file that I need to remove. After going through the same process that this user did, nothing was found when running sudo find / -name .s.PGSQL.5432 -ls

  • I have tried a clean install, and it still doesn't work. Nothing has changed, and the same error messages are appearing.

After removing postgres (following these instructions), I went through the digital ocean tutorial up to the point where it says "You can now access a Postgres prompt immediately by typing: psql".

Other possible factors: (I am at my wit's end, so I'm listing any possible factor that comes to mind)

  1. I am running Ubuntu Xenial (16.04), and postgresql 9.5
  2. I recently moved and am using a new Internet provider. I'm not sure why this would be an issue, but it did require me to change other settings (specifically, to deploy my rails project locally, instead of localhost:3000 I was using my inet addr. In other words, "http://:3000" (where is the number provided for inet by ifconfig). But after moving to a new Internet provider, my inet addr changed.
  3. [Update] Log files:

LOG: could not bind IPv4 socket: Cannot assign requested address
HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
WARNING: could not create listen socket for "localhost"
FATAL: could not create any TCP/IP sockets

At this point I spent several hours to no avail… Please help!!

Best Answer

The /etc/hosts file was incorrectly configured, due to some changes to that file previously. It did not cause trouble immediately, but for some reason changing the file back to defaults (i.e., 127.0.0.1 for the local host and 127.0.1.1 for my machine) solved the problem.

This was discovered by running netstat -tulpn | grep :5432, which gave the following error output: sudo: unable to resolve host <my_machine>. This pointed to the fact that there was a problem with the /etc/hosts file.

For a helpful resource, see this link.