[SOLVED] psql: Could not connect to server: No such file or directory

For handling the databases many may use pSQL and SQL, both have their own advantages and disadvantages. Many of us use pSQL for its faster dealing with dataset and its read-write operations, but the pSQL community faces this pSQL: could not connect to server: No such file or directory error often. It happens when the Postgres server daemon process is not running.

psql: 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"?

Method 1

This error is the misconfiguration of pSQL which crashes the server before the pSQL starts. You can solve this Error sometimes by using ps aux commands or by starting the pSQL again. If this method doesn't work you can try the others mentioned below.

Method 2

  1. Certify the pSQL service by using sudo service postgresql start

  2. Run pg_lsclusters in your terminal it shows- information about all PostgreSQL clusters

  3. In the event that both commands above work fine, then you will receive output in your terminal. 9.6 ------- main -- 5432 online postgres /var/lib/postgresql/9.6/main 9.6 is the Version and others are port status. The '---' signs are used for alignment.

  4. Now note the version and cluster from the previous output, then place it in this command pg_ctlcluster <version> <cluster> start and run it in your terminal so if your command will be pg_ctlcluster 9.6 main start this if you have version.

  5. If you face any problem here pSQL will generate a log, you can access this by /var/log/postgresql/ postgresql-<version>-main.log. If your version is 9.6 then, use the below command. sudo nano /var/log/ postgresql/postgresql-9.6-main.log.

  6. Then your terminal will show the error and try to fix it. The errors are simple only can be fixed easily.

  7. Then restart your pSQL sudo service postgresql restart it should work fine if you fixed the error.

You can fix this by this method also if the previous one is not working

Try to delete the file postmaster.id using command. sudo rm /usr/local/var/postgres/postmaster.pid and restarting the psql may solve the Problem

Method 3 (For MAC Users)

In MAC you can solve this by shutting down the pSQL and updating it. This may solve your problem temporarily

brew services stop postgresql@12
brew services start postgresql@12
brew postgresql-upgrade-database

If you got an issue that the postmaster is running try to delete it from your MAC and restart it.


Further Reading

  1. [SOLVED] psql: FATAL database 'root' does not exist error
  2. Problems with NOT IN operator in pSQL
  3. OSI Model: Layers and its functions

Monitor your software stack for free with Atatus.

Start your free trial
OR
Request a Demo

Free 14-day trial. No credit card required. Cancel anytime.

Ready to see actionable data?

Avail Atatus features for 14 days free-trial. No credit card required. Instant set-up.