Ruby-on-rails postgresql 数据库错误:服务器是否在本地运行并接受 Unix 域套接字“/var/run/postgresql/.s.PGSQL.5432”上的连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28925848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:06:19  来源:igfitidea点击:

error with postgresql datababse : Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

ruby-on-railsrubypostgresqlsockets

提问by adyouri

When I run the rake db:migrateor run the rails scommand, I get the same error:

当我运行rake db:migrate或运行rails s命令时,我收到相同的错误:

Error : 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 get the error in the browser when I try rails s.

当我尝试时,我在浏览器中收到错误rails s

This is my database.yml

这是我的database.yml

default: &default
adapter: postgresql
encoding: unicode

pool: 5

development:
<<: *default
database: books_development




test:
<<: *default
database: books_test



production:
<<: *default
database: books_production
username: abd
password: <%= ENV['BOOKS_DATABASE_PASSWORD'] %>

Note : I have the databases books_development; books_test ; and the postresql are running without problems when I try sudo /etc/init.d/postgresql start

注意:我有数据库books_developmentbooks_test ; 当我尝试时,postresql 运行没有问题sudo /etc/init.d/postgresql start

I did run:

我确实运行了:

create database books_development;
create database books_test; 

in the psql console. And it said that it's done successfully

在 psql 控制台中。它说它已经成功完成

I tried a lot of solutions and I spent yesterday looking for a solution and no solution in the related questions solved my error.

我尝试了很多解决方案,昨天花了很多时间寻找解决方案,但相关问题中没有解决方案解决了我的错误。

I have postgresql-9.4 (the latest) and xubuntu 14.04

我有 postgresql-9.4(最新的)和 xubuntu 14.04

Any Ideas?

有任何想法吗?

采纳答案by Daniel Vérité

The convention for PostgreSQL packaged for Debian or Debian derivatives such as Ubuntu is to use /var/run/postgresqlas the directory for Unix domain sockets. On the other hand the convention for self-compiled postgres client libs is to use /tmp, unless self-configured otherwise.

为 Debian 或 Debian 衍生产品(如 Ubuntu)打包的 PostgreSQL 的约定是/var/run/postgresql用作 Unix 域套接字的目录。另一方面,自编译 postgres 客户端库的约定是使用/tmp,除非以其他方式自行配置。

So the usual root cause of this mismatch between both is a mix of self-compiled client-side stuff with pre-compiled server-side packages (even if client and server are installed on the same machine, client-side and server-side are still distinct and can be out of sync).

因此,两者之间这种不匹配的通常根本原因是自编译的客户端内容与预编译的服务器端包的混合(即使客户端和服务器安装在同一台机器上,客户端和服务器端是仍然不同并且可能不同步)。

Soft-linking from /tmpto this directory as suggested by the asker works except that the link will be lost at every reboot, because in general /tmpis emptied on reboot.

/tmp按照提问者的建议,从这个目录软链接是可行的,但每次重新启动时链接都会丢失,因为通常/tmp在重新启动时会被清空。

A better option would be to add as an entry in database.yml:

更好的选择是添加为条目database.yml

  • either host: /tmpif the real socket path is /tmp(self-compiled server, packaged client)

  • or host: /var/run/postgresqlif the real socket path /var/run/postgresql/(packaged server, self-compiled client).

  • 无论host: /tmp是否真正插座路径/tmp(自编服务器,客户端打包)

  • 或者host: /var/run/postgresql如果是真实的套接字路径/var/run/postgresql/(打包的服务器,自编译的客户端)。

When the value in the host field starts with a slash character, the postgres library knows that it's the location of a directory for local sockets rather than a hostname. The filename inside the directory .s.PGSQL.portnumberis generated and must not be specified, only the directory.

当主机字段中的值以斜杠字符开头时,postgres 库知道它是本地套接字的目录位置而不是主机名。目录内的文件名.s.PGSQL.portnumber是生成的,不能指定,只能指定目录。

Another possibility is to configure the self-compiled software packages as closely as possible to Debian, overriding the defaults as they do.

另一种可能性是将自编译软件包配置得尽可能接近 Debian,像它们一样覆盖默认值。

回答by descript

I had the same Is the server running locally and accepting connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?error when typing psqlinto the postgres user in Ubuntu 14.04. I could not find an existing working solution.

在 Ubuntu 14.04 中Is the server running locally and accepting connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?输入psqlpostgres 用户时,我遇到了同样的错误。我找不到现有的工作解决方案。

The short answer for me was: my install made a var/pgsql_socketdirectory but no configuration files knew about it.

对我来说,简短的回答是:我的安装创建了一个var/pgsql_socket目录,但没有配置文件知道它。

1) Find the postgres.conffile (it was in etc/postgresql/9.6/mainfor me)
2) change to listen_addresses = '*'
3) add another unix socket directory
unix_socket_directories = '/var/run/postgresql, /var/pgsql_socket' # comma-separated list of directories
4) at this point, sudo service postgresql start attempted to start but did not have authority to create the lock file.
* The PostgreSQL server failed to start. Please check the log output: 2016-10-05 17:14:55 CEST [28472-1] FATAL: could not create lock file "/var/pgsql_socket/.s.PGSQL.5432.lock": Permission denied 2016-10-05 17:14:55 CEST [28472-2] LOG: database system is shut down
5) Change permissions ( found from Mark Berry's comment here)
$ sudo chown root.postgres /var/pgsql_socket
$ sudo chmod g+wx /var/pgsql_socket
6) sudo service postgresql start
sudo -i -u postgres
psql

1)找到postgres.conf文件(它是etc/postgresql/9.6/main我的)
2)更改为listen_addresses = '*'
3)添加另一个unix套接字目录
unix_socket_directories = '/var/run/postgresql, /var/pgsql_socket' # comma-separated list of directories
4)此时, sudo service postgresql start 尝试启动但没有创建锁定文件的权限。
* The PostgreSQL server failed to start. Please check the log output: 2016-10-05 17:14:55 CEST [28472-1] FATAL: could not create lock file "/var/pgsql_socket/.s.PGSQL.5432.lock": Permission denied 2016-10-05 17:14:55 CEST [28472-2] LOG: database system is shut down
5) 更改权限(从Mark Berry 的评论中找到
$ sudo chown root.postgres /var/pgsql_socket
$ sudo chmod g+wx /var/pgsql_socket
6)sudo service postgresql start
sudo -i -u postgres
psql

That finally worked for me

这最终对我有用

回答by adyouri

I solved It . I Just created a softlink using :

我解决了它。我刚刚使用以下方法创建了一个软链接:

sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432

sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432

and then edited the

然后编辑了

/etc/postgresql/9.4/main/pg_hba.conf

/etc/postgresql/9.4/main/pg_hba.conf

( If you have another version of postgresql you have to change 9.4 in the path)

(如果你有另一个版本的 postgresql 你必须在路径中更改 9.4)

From:

从:

local all postgres peer

local all postgres peer

To:

到:

local all postgres md5

local all postgres md5

回答by bogdanvlviv

Solution:

解决方案:

Try this

尝试这个

export LC_ALL="en_US.UTF-8"

and this. (9.3is my current PostgreSQL version. Write your version!)

和这个。(9.3是我当前的 PostgreSQL 版本。写下你的版本!)

sudo pg_createcluster 9.3 main --start

回答by David Weber

The exact same symptom can be caused by a stale lock file /var/run/postgresql/.s.PGSQL.5432.lock. One of the symptoms of this is psqlreporting

过时的锁定文件可能会导致完全相同的症状/var/run/postgresql/.s.PGSQL.5432.lock。这种情况的症状之一是psql报告

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

psql:无法连接到服务器:没有这样的文件或目录服务器是否在本地运行并接受 Unix 域套接字“/var/run/postgresql/.s.PGSQL.5432”上的连接?

even though there is clearly a socket with this path available as reported by netstat -lp --protocol=unix | grep postgres

即使显然有一个可用的此路径的套接字,如报告的那样 netstat -lp --protocol=unix | grep postgres

The problem can be solved by removing the lock file and restarting postgresql. This is definitely less invasive than a purge and re-install.

问题可以通过删除锁定文件并重新启动postgresql来解决。这绝对比清除并重新安装的侵入性小。

sudo rm /var/run/postgresql/.s.PGSQL.5432.lock
sudo service postgresql restart

回答by Sumon Sarker

That means your Postgresserver is not running.

这意味着您的Postgres服务器没有运行。

Check PostgresService status from Terminal

从终端检查Postgres服务状态

sudo service postgresql status

Enable PostgresService, If not started

启用Postgres服务,如果没有启动

sudo service postgresql start

OR

或者

sudo service postgresql restart

Now your command should work, If PostgresService is successfully started.

现在您的命令应该可以工作,如果Postgres服务已成功启动。

回答by tobasti

When I run into this error, my postgres server was acutally listening on a different port (5433). To solve this, add a line in your database.yml to instruct rails to use the same:

当我遇到此错误时,我的 postgres 服务器实际上正在侦听不同的端口 (5433)。要解决此问题,请在 database.yml 中添加一行以指示 rails 使用相同的内容:

port: 5433

回答by Noushad

Running pg_lsclusterswill list all the postgres clusters running on your device eg:

运行pg_lsclusters将列出您设备上运行的所有 postgres 集群,例如:

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

if the status is down run

如果状态为 down run

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

If this process is not successfull it will throw the error. My error was(You can see the error log on /var/log/postgresql/postgresql-9.6-main.log)

如果此过程不成功,它将抛出错误。我的错误是(你可以看到错误日志/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`

make sure that postgresis the owner of /var/lib/postgresql/version_no/maineg: sudo chown postgres -R /var/lib/postgresql/9.6/main/

确保这postgres/var/lib/postgresql/version_no/main例如的所有者:sudo chown postgres -R /var/lib/postgresql/9.6/main/

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 fixing the permissions

这发生在我身上,结果我错误地从“ssl-cert”组中删除了 Postgres 用户。运行以下代码以修复用户组问题并修复权限

#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 postgres restart