174

I typed psql and I get this:

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

I used sudo netstat -nlp | grep 5432 to see the status but nothing showed. And I searched online, somebody told me to modify pg_hba.conf but I can't locate this file. And I also tried this command sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432. It can't work.

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
hidemyname
  • 3,791
  • 7
  • 27
  • 41

35 Answers35

105

The error states that the psql utility can't find the socket to connect to your database server. Either you don't have the database service running in the background, or the socket is located elsewhere, or perhaps the pg_hba.conf needs to be fixed.

Step 1: Verify that the database is running

The command may vary depending on your operating system. But on most *ix systems the following would work, it will search for postgres among all running processes

ps -ef | grep postgres

On my system, mac osx, this spits out

501   408     1   0  2Jul15 ??         0:21.63 /usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres -r /usr/local/var/postgres/server.log

The last column shows the command used to start the server, and the options.

You can look at all the options available to start the postgres server using the following.

man postgres

From there, you'd see that the options -D and -r are respectively the datadir & the logfilename.

Step 2: If the postgres service is running

Use find to search for the location of the socket, which should be somewhere in the /tmp

sudo find /tmp/ -name .s.PGSQL.5432

If postgres is running and accepting socket connections, the above should tell you the location of the socket. On my machine, it turned out to be:

/tmp/.s.PGSQL.5432

Then, try connecting via psql using this file's location explicitly, eg.

psql -h /tmp/ dbname

Step 3: If the service is running but you don't see a socket

If you can't find the socket, but see that the service is running, Verify that the pg_hba.conf file allows local sockets.

Browse to the datadir and you should find the pg_hba.conf file.

By default, near the bottom of the file you should see the following lines:

# "local" is for Unix domain socket connections only
local       all       all       trust

If you don't see it, you can modify the file, and restart the postgres service.

Haleemur Ali
  • 26,718
  • 5
  • 61
  • 85
  • 4
    I was concerned that this may not apply to my situation. I had no pg_hba.conf at all anywhere, but still had a locally functioning postgresql server. Using this answer, and consulting https://www.postgresql.org/docs/9.4/static/auth-pg-hba-conf.html for additional remote access options worked great. Thanks @Haleemur Ali! – mightypile Nov 11 '16 at 15:32
  • 3
    Command to restart postgres: https://stackoverflow.com/a/52610486/9843399 – Caleb Syring Dec 10 '19 at 17:45
  • 2
    I followed all these instructions as my service showed the same message after I tried to `sudo -u postgres psql`. Since I am a beginner at Linux and command line in general, after a lot of `man` here this `man` that I finally found that a simple run of `find /var/run/ | grep postgres` and the process would show up, right? I found this: `/var/run/postgresql/.s.PGSQL.5433` It's on 5433! `sudo -u postgres -p 5433` and BOOM - you're ready to go again :) – W1ck3d Feb 17 '22 at 15:17
  • 4
    step one gives me `user 3251 2963 0 17:31 pts/4 00:00:00 grep --color=auto postgres` on Ubuntu20.04 on wsl2 (the postgres is colored red). what does this mean? – tenshi Mar 28 '22 at 09:38
  • 1
    @tenshi this is the search command (`grep`) that you ran (including the default options of `--color=auto`). If there are no other lines then postgresql is not running. – PhilT Aug 01 '22 at 16:36
  • You can get to this page simply because postgres is not running on default port and then this answer is a bit misleading. You should change the port by a wildcard * in the find. If the port is not default this command will help : PGPORT=5433; export PGPORT – bokan Aug 05 '22 at 10:19
  • This is what I called, chain of thought reasoning. Thanks Man! are you sure, you are not an LLM? – Luillyfe Jun 30 '23 at 16:05
  • What if the server isn't running? - This happed to me. First try run the server manually to see if that is really the case, by running `postgres -D /usr/local/pgsql/data`. If the server is running and you can run psql normally, then you just need to set up the server running whenever your system is booting up by using `systemd`. I followed [this](https://www.postgresql.org/docs/current/server-start.html). – Danh Le Aug 21 '23 at 12:07
97

If there is no error in starting the Postgres service, follow these steps

Step 1: Running pg_lsclusters will list all the Postgres clusters running on your device

eg:

Ver Cluster Port Status Owner    Data directory               Log file
9.6 main    5432 online postgres /var/lib/postgresql/9.6/main /var/log/postgresql/postgresql-9.6-main.log

most probably the status will be down in your case. If not restart PostgreSQL service

Step 2: Restart the pg_ctlcluster

#format is pg_ctlcluster <version> <cluster> <action>
sudo pg_ctlcluster 9.6 main start

#restart PostgreSQL service
sudo service postgresql restart

Step 3: Step 2 failed and threw an error

If restarting pg_lsclusters was not successful, it will throw an error. My error was(You can see the errors in the logs /var/log/postgresql/postgresql-9.6-main.log)

FATAL: could not access private key file "/etc/ssl/private/ssl-cert-snakeoil.key": Permission denied
Try adding `postgres` user to the group `ssl-cert`

Step 4: check ownership of postgres

Make sure that postgres is the owner of /var/lib/postgresql/version_no/main eg: sudo chown postgres -R /var/lib/postgresql/9.6/main/

Step 5: Check postgres user belongs to ssl-cert user group

It happened to me and it turned out that I removed erroneously the Postgres user from "ssl-cert" group. Run the below code to fix the user group issue and for fixing the permissions

#set user to group back with
sudo gpasswd -a postgres ssl-cert

# Fixed ownership and mode
sudo chown root:ssl-cert  /etc/ssl/private/ssl-cert-snakeoil.key
sudo chmod 740 /etc/ssl/private/ssl-cert-snakeoil.key

# now postgresql starts! (and install command doesn't fail anymore)
sudo service postgresql restart
Noushad
  • 6,063
  • 3
  • 25
  • 28
60

I experienced this issue when working with PostgreSQL on Ubuntu 18.04.

I checked my PostgreSQL status and realized that it was running fine using:

sudo systemctl status postgresql

I also tried restarting the PotgreSQL server on the machine using:

sudo systemctl restart postgresql

but the issue persisted:

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

Following Noushad' answer I did the following:

List all the Postgres clusters running on your device:

pg_lsclusters

this gave me this output in red colour, showing that they were all down and the status also showed down:

Ver Cluster Port Status Owner    Data directory              Log file
10  main    5432 down   postgres /var/lib/postgresql/10/main /var/log/postgresql/postgresql-10-main.log
11  main    5433 down   postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
12  main    5434 down   postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log

Restart the pg_ctlcluster for one of the server clusters. For me I restarted PG 10:

sudo pg_ctlcluster 10 main start

It however threw the error below, and the same error occurred when I tried restarting other PG clusters:

Job for postgresql@10-main.service failed because the service did not take the steps required by its unit configuration.
See "systemctl status postgresql@10-main.service" and "journalctl -xe" for details.

Check the log for errors, in this case mine is PG 10:

sudo nano /var/log/postgresql/postgresql-10-main.log

I saw the following error:

2020-09-29 02:27:06.445 WAT [25041] FATAL:  data directory "/var/lib/postgresql/10/main" has group or world access
2020-09-29 02:27:06.445 WAT [25041] DETAIL:  Permissions should be u=rwx (0700).
pg_ctl: could not start server
Examine the log output.

This was caused because I made changes to the file permissions for the PostgreSQL data directory.

I fixed it by running the command below. I ran the command for the 3 PG clusters on my machine:

sudo chmod -R 0700 /var/lib/postgresql/10/main
sudo chmod -R 0700 /var/lib/postgresql/11/main
sudo chmod -R 0700 /var/lib/postgresql/12/main

Afterwhich I restarted each of the PG clusters:

sudo pg_ctlcluster 10 main start
sudo pg_ctlcluster 11 main start
sudo pg_ctlcluster 12 main start

And then finally I checked the health of clusters again:

pg_lsclusters

this time around everything was fine again as the status showed online:

Ver Cluster Port Status Owner    Data directory              Log file
10  main    5432 online postgres /var/lib/postgresql/10/main /var/log/postgresql/postgresql-10-main.log
11  main    5433 online postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
12  main    5434 online postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log

That's all.

I hope this helps

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
  • 2
    thank you very much, this line `/var/log/postgresql/postgresql-10-main.log` just saved me. One can find a lot of information about the log files under `/var/lib/postgres/13/main/log/` but after 3 days of research I did not found anything about the main log file and now I see in the main log that my `server.crt` ee key too small. Just creating new `crt` with 2048 bits worked for me – khashashin Apr 15 '21 at 22:49
  • In my case I simply deleted the clusters for older versions of postgres as I had no worries about data loss. After that psql could connect fine with latest version server. 1) List all clusters: ps_lscluster 2) delete older version clusters: pg_dropcluster – Farrukh Najmi Jan 17 '22 at 17:33
44

In my case the following worked for starting postgres after running into the error

sudo service postgresql start
sudo su - postgres
psql
Aloha Churchill
  • 549
  • 4
  • 5
33

I have encountered a similar issue a couple of times. Normally I just do a fresh installation of PostgreSQL following this tutorial and that solves the problem at the expense of losing data.

I was determined on getting real fix today. Restarting PostgreSQL resolved it on ubuntu. sudo /etc/init.d/postgresql restart

Faith Nassiwa
  • 501
  • 5
  • 6
16

If you are using WSL 2 use this :

sudo service postgresql start
KNTY
  • 351
  • 3
  • 11
15

I was facing same problem and

sudo su - postgres
initdb --locale en_US.UTF-8 -D /var/lib/postgres/data
exit
sudo systemctl start postgresql
sudo systemctl status postgresql

This worked for me.

Aashutosh Rathi
  • 763
  • 2
  • 13
  • 28
9

Solved it! Although I don't know what happened, but I just deleted all the stuff and reinstalled it. This is the command I used to delete it sudo apt-get --purge remove postgresql\* and dpkg -l | grep postgres. The latter one is to find all the packets in case it is not clean.

hidemyname
  • 3,791
  • 7
  • 27
  • 41
  • 25
    Note for other readers: that this deletes your configuration (at least), so don't just blindly run this command. In most cases there's something specific wrong, like the server simply not having been started up, and you should fix the specific problem rather than purging and reinstalling everything. – Craig Ringer Jul 27 '15 at 07:27
  • Thanks, my postgres on an ec2 instance wouldn't start anymore. That did the trick. Since it was a dev server I didn't mind losing the data. – Mike Birkhoff Mar 04 '20 at 12:46
5

quick howto on debian to remotely access postgres database on server from the psql client: (the changed config is doc'd in the files):

  1. edit /etc/postgresql/10/main/postgresql.conf with listen_address *
  2. edit /etc/postgresql/10/main/pg_hba.conf and add line in the end with host all all 0/0 md5
  3. create login role postgres=# CREATE ROLE remoteuser LOGIN WITH PASSWORD 'foo'
  4. sudo /etc/init.d/postgresql restart changes take effect

  5. login from clientside with psql --host=ipofserver --port=5432 --username=remoteuser --password --dbname=mydb

  6. the password is interactivly asked which in this case is foo
droid192
  • 2,011
  • 26
  • 43
4

I resolved this problem by checking my file system the disk was completely full, and so database could not start up

connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432" ?

I tried series of troubleshooting, until when i checked my disk usage and found that it was full, 100% usage,

df -h
cd /var/log/odoo/
cat /dev/null > odoo-server.log
reboot
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • I wouldn't have thought of that, so thank you. I have a script that makes a backup of all my databases every night and eventually the disk was full and it was causing this error. – Dreamscape Dec 14 '20 at 07:48
3

The error means that the Postgres server is not running. Try starting it:

sudo systemctl start postgresql

Make sure that the server starts on boot:

sudo systemctl enable postgresql
georgeawg
  • 48,608
  • 13
  • 72
  • 95
sbandra
  • 31
  • 2
  • Was about to comment this as it worked for me immediately and then found yours. Let's get this answer unburied. – Joe Roddy Sep 30 '19 at 19:31
3

if you are using windows subsystem for linux and Ruby on Rails then check your postgres is running in which port using this command sudo nano /etc/postgresql/12/main/postgresql.conf if it is in port 5433 then go to database.yml file and add port:5433 in there and then run command sudo service postgresql start i have resolved my issue like this

Aneesh
  • 101
  • 9
3

I believe I fell into this issue after uninstalling PostgreSQL 11 and 12 while already installed 13 on on Ubuntu 21. I just solved this issue by sudo nano /etc/postgresql/13/main/postgresql.conf, and found that port = 5433 (don't know why). So I changed port = 5432. Then problem solved.

qamnott
  • 691
  • 5
  • 5
  • I met the same problem... is pg 13 using 5433 port ? – Siwei Jan 25 '22 at 01:47
  • 1
    Probably by default it will try to use 5432, but it was likely that port 5432 already in use by existing process (such as previous version of PostgreSQL or similar process "detected",) which make it attempt to choose 5433 (incrementally). – qamnott Feb 17 '22 at 08:43
3

In Ubuntu 20.04.4 LTS. I was worried about the socket connection because i was using:

psql -U postgres db_omm < db.pgsql

and the error was: Error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "postgres"

But the authentication uses the user permissions. So even if you specify the user and you are in root or other one, you have to switch to postgres

su - postgres

Then it worked!

srttowers
  • 73
  • 8
2

I had the same issue on Devuan ascii (maybe Debian, too?). The config file /etc/postgresql/9.6/main/postgresql.conf contains a directive unix_socket_directories which points to /var/run/postgresql by default. Changing it to /tmp, where most clients look by default, fixed it for me.

Markus
  • 2,412
  • 29
  • 28
1

My issue with this error message was in wrong permissions on key and pem certificates, which I have manipulated. What helped me a lot was: /var/log/postgresql/postgresql-9.5-main.log where are all the errors.

Fide
  • 1,127
  • 8
  • 7
1

I had the same problem. It seems that there is no socket when there is no cluster.

The default cluster creation failed during the installation because no default locale was set.

JulienCC
  • 446
  • 2
  • 11
  • Thanks, this fixed my issue! I lost several hours searching for a solution and was thinking about asking a new question but it was worth checking less popular answers. Indeed the command pg_lsclusters was complaining about missing locale variables that were not set so I had to set them in /etc/default/locale. Maybe this is a bug that should be reported to PostgreSQL, there should be a fallback when there is no default locale set. – baptx Nov 10 '22 at 14:43
1

In my case I had this error, /var/run/postgresql/.s.PGSQL.5433 (note, one number up from the file it was looking for, .s.PGSQL.5432) was present. Tried the instructions at the top of this page but nothing worked.

Turns out there was an old directory for PostGreSQL 12 config files in /etc/postgresql/12, which I deleted, which solved the issue.

bguiz
  • 27,371
  • 47
  • 154
  • 243
0

Just want to make a small addition: if your instance is complaining on a socket, you can also check unix_socket_directories at /data/postgresql.conf file which could have been set to /tmp, for example, if you have used a 3rd party distribution. You can change it to /var/run/postgresql and restart the service. That may also require creating a postgresql dir at /var/run and subsys/postgresql-9.6 at /var/lock if those doesn't already exist (worked for me with postgresql 9.6).

Anton Zvonovsky
  • 313
  • 2
  • 6
  • 16
0

During fresh installation of postgresql. By default, user name and password is assigned as "postgres". The feature this RDBMS provides is to add role for new user and create database. If you are getting such errors:

  1. login in by default username:

    root@kalilinux:~# sudo -i -u postgres

  2. ype psql for interactive prompt

    postgres@kalilinux:~$ psql

  3. To quit from prompt use

    \q

  4. To create new user role

    postgres@kalilinux:~$ createuser --interactive

Now you are in interacive psql shell. Enjoy. Dont forget to login in from your username and type psql for shell.

Suman Astani
  • 1,181
  • 11
  • 16
0

It can cause anything for example, my issue was caused for typo error on configuration files. Some of people says caused by certificate files, another group says caused by unmatched locals.

If you cant find any solution about your issue, remove postgres and reinstall it.This is the best solution.

sayhan
  • 1,168
  • 1
  • 16
  • 22
0

I had the similar issue and the problem was in the config file pg_hba.conf. I had earlier made some changes which was causing the server to error out while trying to start it. Commenting out the extra additions solved the problem.

0

Restarting the instance worked for me. Also, as mentioned in some other post psql -h '/tmp' worked as well before the restart. But post restart psql directly started working. So, probably some file ownership issues that got reset with the restart is what I am thinking.

balaks80
  • 136
  • 1
  • 7
0

If your service is not secure, this may be the reason

vi /etc/postgresql/11/main/pg_hba.conf
  1. open hba config file, this config file usualy located in the etc directory.
host    all   all    localhost trust   md5
  1. you can remove the trust keyword

  2. save pg_hba.conf

  3. sudo service postgresql restart.

Erdal
  • 64
  • 9
0

Verify that Postgres is running using:

ps -ef | grep postgres
root@959dca34cc6d:/var/lib/edb# ps -ef|grep postgres
enterpr+    476  1  0 06:38 ?        00:00:00 /usr/lib/edb-as/11/bin/edb-postgres -D /var/lib/edb-as/11/main2 -c config_file=/etc/edb-as/11/main2/postgresql.conf

Check for data directory and postgresql.conf.

In my case data directory in -D was different than that in postgresql.conf

So I changed the data directory in postgresql.conf and it worked.

marcolz
  • 2,880
  • 2
  • 23
  • 28
0

I could resolve this by setting the right permissions to datadir. It should be

chmod 700 /var/lib/postgresql/10/main
chown postgres.postgres /var/lib/postgresql/10/main
0

In my case, the service was running but the cluster was down and psql wouldn't start. My configuration files looked perfect but it kept throwing configuration errors and seemed to ignore the changes I was making.

It turns out that whenever you use ALTER SYSTEM SET ... syntax, PostgreSQL writes to a file called postgresql.auto.conf. That file is read in addition to the regular postgresql.conf and pg_hba.conf files. In my distribution of Ubuntu (18.04), they are in different folders(!):
- pg_hba.conf and postgresql.conf are both in /etc/postgresql/12/main
- The auto-generated file is /var/lib/postgresql/12/main/postgresql.auto.conf

I had tried to change the configuration using ALTER SYSTEM SET listen_addresses = <my-ip>, but had made a mistake and that created a broken "ghost" configuration that I couldn't find. As soon as I erased the offending line in postgresql.auto.conf, it fixed everything.

John
  • 1,018
  • 12
  • 19
0

In my case I saw this error and postgres was not running.

The problem was that the instalation failed to create the required cluster.

The solution was to create the folder /etc/postgres/{postgresql-version}/main

and then create the cluster with:

pg_createcluster {postgresql-version} main

After that wiht just restarting the postgresql service everything should work.

Duilio
  • 876
  • 1
  • 10
  • 15
0

OK in my case, I uninstalled postgres 10 , but not successfully ,some pg10 file/process still remains there.

and then I installed postgres 13, so pg 13 think there is another pg running on port 5432, so it's using 5433.

solution: purge all your pg via: sudo apt-get --purge remove postgresql postgresql-* and then re-install it. ( caution: this will remove all your existing data )

Siwei
  • 19,858
  • 7
  • 75
  • 95
0

on rebooting the server i found these in 2 locations

srwxrwxrwx 1 postgres postgres 0 Nov 10 18:44 .s.PGSQL.5432 -rw------- 1 postgres postgres 48 Nov 10 18:44 .s.PGSQL.5432.lock

both these files were present in below locations and were not present before reboot /var/run/postgresql/ /tmp/

make sure these files with the corresponding permissions are available and start the psql

Naresh
  • 21
  • 1
0

For me, I had to set up the credentials in the config/database.yml under the default database.

username: <%= ENV["PG_USERNAME"] %>
password: <%= ENV["PG_PASSWORD"] %>

Picture of code to be added

ConorFair
  • 25
  • 4
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 02 '23 at 06:54
-1

So for me and my pals working on a Node.js app (with Postgres and Sequelize), we had to

  1. brew install postgresql (one of us was missing postgres, one of us was not, and yet we were getting the same error msg as listed above)

  2. brew services start postgresql **** (utilize Homebrew to start postgres)

  3. createdb <name of database in config.json file>

  4. node_modules/.bin/sequelize db:migrate

  5. npm start

-1

This command works for me on Ubuntu machine

sudo su -postgres

it would request for your password to be entered, most likely your default password.

Gabriel soft
  • 432
  • 3
  • 7
-1

This is problem occurs with me sometime we add some back up folder, log file folder , scripts ,some certain dependency in postgresql.conf file if this no exist they cause may problem

-1

I know my answer is too late, but I think it can help someone.

I did get his issue when I was modifying the pg_hba.conf to recover password. I am running Debian GNU/Linux. Here's what I did:

  • backup pg_hba.conf to pg_hba.conf.bk
  • edit the file
  • restart the server
  • everything ok

But, when I want to recover the backup file:

  • rename pg_hba.conf to pg_hba.conf_no_pass
  • rename pg_hba.conf.bk to pg_hba.conf
  • restart the server
  • I get the error above.

So, to fix, I just renamed pg_hba.conf_no_pass to pg_hba.conf and edit the file as it was before. After restarting the server, everything was just fine.

Abel LIFAEFI MBULA
  • 95
  • 1
  • 1
  • 10