[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"?
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.
Certify the pSQL service by using
sudo service postgresql start
pg_lsclustersin your terminal it shows- information about all PostgreSQL clusters
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/main9.6 is the Version and others are port status. The '---' signs are used for alignment.
Now note the version and cluster from the previous output, then place it in this command
pg_ctlcluster <version> <cluster> startand run it in your terminal so if your command will be
pg_ctlcluster 9.6 main startthis if you have version.
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.
Then your terminal will show the error and try to fix it. The errors are simple only can be fixed easily.
Then restart your pSQL
sudo service postgresql restartit 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.