postgresql 数据库错误:服务器是否在本地运行并接受 Unix 域套接字“/var/run/postgresql/.s.PGSQL.5432"上的连接? [英] error with postgresql datababse : Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
问题描述
当我运行 rake db:migrate
或运行 rails s
命令时,我得到同样的错误:
When I run the rake db:migrate
or run the rails s
command, I get the same error:
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"?
当我尝试 rails s
时,我在浏览器中遇到错误.
I get the error in the browser when I try rails s
.
这是我的数据库.yml
This is my 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'] %>
注意:我有数据库books_development
;books_test代码> ;当我尝试
sudo/etc/init.d/postgresql start
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
我确实跑了:
create database books_development;
create database books_test;
在 psql 控制台中.然后说成功了
in the psql console. And it said that it's done successfully
我尝试了很多解决方案,昨天我一直在寻找解决方案,但相关问题中没有解决方案解决了我的错误.
I tried a lot of solutions and I spent yesterday looking for a solution and no solution in the related questions solved my error.
我有 postgresql-9.4(最新的)和 xubuntu 14.04
I have postgresql-9.4 (the latest) and xubuntu 14.04
有什么想法吗?
推荐答案
为 Debian 或 Debian 衍生产品(如 Ubuntu)打包的 PostgreSQL 的约定是使用 /var/run/postgresql
作为目录Unix 域套接字.另一方面,自编译 postgres 客户端库的约定是使用 /tmp
,除非自行配置.
The convention for PostgreSQL packaged for Debian or Debian derivatives such as Ubuntu is to use /var/run/postgresql
as 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.
因此,两者之间这种不匹配的通常根本原因是自编译的客户端内容与预编译的服务器端包的混合(即使客户端和服务器安装在同一台机器上,客户端和服务器-side 仍然不同,可能不同步).
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).
按照提问者的建议,从 /tmp
软链接到这个目录是可行的,但每次重启都会丢失链接,因为通常 /tmp
被清空重新启动.
Soft-linking from /tmp
to this directory as suggested by the asker works except that the link will be lost at every reboot, because in general /tmp
is emptied on reboot.
更好的选择是在 database.yml
中添加一个条目:
A better option would be to add as an entry in database.yml
:
任一
host:/tmp
如果真正的socket路径是/tmp
(自编译服务器,打包客户端)
either
host: /tmp
if the real socket path is/tmp
(self-compiled server, packaged client)
or host:/var/run/postgresql
如果是真正的socket路径/var/run/postgresql/
(打包服务器,自编译客户端).
or host: /var/run/postgresql
if the real socket path /var/run/postgresql/
(packaged server, self-compiled client).
当主机字段中的值以斜杠字符开头时,postgres 库知道它是本地套接字的目录位置而不是主机名.目录.s.PGSQL.portnumber
内的文件名是生成的,不能指定,只能指定目录.
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.portnumber
is generated and must not be specified, only the directory.
另一种可能性是尽可能接近 Debian 配置自编译软件包,像它们一样覆盖默认值.
Another possibility is to configure the self-compiled software packages as closely as possible to Debian, overriding the defaults as they do.
这篇关于postgresql 数据库错误:服务器是否在本地运行并接受 Unix 域套接字“/var/run/postgresql/.s.PGSQL.5432"上的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!