Postgresql 和 django - Unix 域套接字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10829464/
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
Postgresql and django - Unix domain socket
提问by user1407540
i spend couple of hours to solve these problem but did not reach anything. There are several topics about this problem on the net but none of them says an absolute thing to solve this.
我花了几个小时来解决这些问题,但没有达到任何目的。网上有几个关于这个问题的话题,但没有一个说绝对可以解决这个问题。
I just installed postgresql in order to use it on my django project.
我刚刚安装了 postgresql 以便在我的 django 项目中使用它。
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2", # Add "postgresql_psycopg2", "postgresql", "mysql", "sqlite3" or "oracle".
"NAME": "name", # Or path to database file if using sqlite3.
"USER": "postgres", # Not used with sqlite3.
"PASSWORD": "pass", # Not used with sqlite3.
"HOST": "", # Set to empty string for localhost. Not used with sqlite3.
"PORT": "", # Set to empty string for default. Not used with sqlite3.
}
}
this is my settings.py and the error is that
这是我的 settings.py 错误是
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"?
Did anyone have a solution about it ?
有没有人有解决方案?
回答by Tometzky
You need to locate a postgres socket. Check main postgres process pid (ps auxw | grep postgres
) and list its open unix sockets (lsof -p [PID_OF_POSTGRES_PROCESS] | grep unix
). Write this path to HOST
option in settings.py
.
您需要找到一个 postgres 套接字。检查主 postgres 进程 pid ( ps auxw | grep postgres
) 并列出其打开的 unix 套接字 ( lsof -p [PID_OF_POSTGRES_PROCESS] | grep unix
)。写这个路径HOST
中选择settings.py
。
Installing Postgres from distribution package (apt-get install postgresql
) would be much easier (for example empty HOST in settings.py
would work) and safer as your distribution will install security updates for you.
从分发包 ( apt-get install postgresql
)安装 Postgres会容易得多(例如,空的 HOSTsettings.py
可以工作)并且更安全,因为您的分发会为您安装安全更新。
回答by Brendan Quinn
Here's another possibility: if you installed a new version of PostgreSQL over an existing version, the installer might have assigned it a non-standard port number.
这是另一种可能性:如果您在现有版本的基础上安装了新版本的 PostgreSQL,安装程序可能会为其分配一个非标准端口号。
Django expects PostgreSQL to listen on port 5432. Even if you're using Unix sockets, the socket is named after the port number so this still matters!
Django 希望 PostgreSQL 侦听端口 5432。即使您使用的是 Unix 套接字,套接字也是以端口号命名的,所以这仍然很重要!
Check /etc/postgresql/<version>/main/postgresql.conf
for the line port = nnnn
. If that number is not 5432, then that's your problem. Another indicator is that your socket file in /var/run/postgresql
will be called something like .s.PGSQL.5433
, using the non-standard port number.
检查/etc/postgresql/<version>/main/postgresql.conf
线路port = nnnn
。如果该数字不是 5432,那就是您的问题。另一个指标是您的套接字文件/var/run/postgresql
将.s.PGSQL.5433
使用非标准端口号调用类似。
To fix it, you can either edit the port number in postgresql.conf
to use the default (5432), or if you need it to run on a non-standard port number, then set DATABASE_PORT = 'nnnn'
in your django settings.py
.
要修复它,您可以编辑端口号postgresql.conf
以使用默认值 (5432),或者如果您需要它在非标准端口号上运行,则DATABASE_PORT = 'nnnn'
在您的 django 中设置settings.py
。
Thanks to Erik Forsbergfor the pointer!
感谢Erik Forsberg的指点!
回答by NoelProf
On contemporary Fedora systems, a private temp directory feature has been added that will cause this symptom. https://fedoraproject.org/wiki/Features/ServicesPrivateTmp
在现代 Fedora 系统上,添加了一个私有临时目录功能会导致此症状。https://fedoraproject.org/wiki/Features/ServicesPrivateTmp
This feature causes web apps to use a tmp directory different from the system /tmp directory where the default PostgreSQL domain socked is located.
此功能会导致 Web 应用程序使用与默认 PostgreSQL 域 socked 所在的系统 /tmp 目录不同的 tmp 目录。
To change the systemd directive for this feature for Apache-httpd on Fedora, edit the file /usr/lib/systemd/system/httpd.service
and change PrivateTmp=true
to PrivateTmp=false
要在 Fedora 上为 Apache-httpd 更改此功能的 systemd 指令,请编辑该文件/usr/lib/systemd/system/httpd.service
并将其更改PrivateTmp=true
为PrivateTmp=false
Alternatively, you can avoid using domain sockets to avoid this issue.
或者,您可以避免使用域套接字来避免此问题。