Skip to content

Instantly share code, notes, and snippets.

@mejiaro
Last active November 3, 2023 06:49
Show Gist options
  • Save mejiaro/4d44689b491dcfc6ee8e05aea70a36fa to your computer and use it in GitHub Desktop.
Save mejiaro/4d44689b491dcfc6ee8e05aea70a36fa to your computer and use it in GitHub Desktop.
Fixing Postgresql's "could not connect to server" on Linux Mint (or Ubuntu)

Got the dreaded, only-macOs documented "No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432" but you are using wither Mint or Ubuntu?

Here's how to fix it.

  1. verify the contents of /etc/postgresql/9.5/main/pg_hba.conf

  2. if the file is empty, add this conf

# TYPE  DATABASE        USER            ADDRESS             METHOD
# "local" is for Unix domain socket connections only
local   all             all                                 trust

# IPv4 local connections:
host    all             all             0.0.0.0/32          md5
host    all             all             127.0.0.1/32        trust
host    all             all             all                 trust

# IPv6 local connections:
host    all             all             ::1/128             trust

# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            ident
#host    replication     postgres        ::1/128                 ident
  1. stop postgresql service with sudo /etc/init.d/postgresql stop

  2. start postgresql service with sudo /etc/init.d/postgresql start

  3. verify you can connect to the server with psql

@MauricioTRP
Copy link

MauricioTRP commented Jun 6, 2023

This doesn't help me

I've made a symlink from /var/run/postgresql/.s.PGSQL.5432 to tmp /tmp/.s.PGSQL.5432 that worked for a while.

But now postgres isn't creating the file /var/run/postgresql/.s.PGSQL.5432 so now I cant create the link

I've looked for the file .s.PGSQL.5432 with no success.
Any clues?

I must say that, I can connect using psql -U postgres, and cant connect a rails app (no passwords needed in order to access the locals DB)

@mejiaro
Copy link
Author

mejiaro commented Jun 8, 2023

sorry, I haven't had this issue for some time now. If you can connect using Postgres but not rails sounds like the error might be on the database.yml config.

You sometimes get this error too when there's a lingering .pid file if the server wasn't shutdown properly.

Good luck

@abbos1711
Copy link

thanks for useful info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment