Django Postgres导入错误:服务器是否在本地运行并接受Unix域套接字上的连接?

时间:2015-11-17 23:45:03

标签: python django postgresql sockets

我在共享的bluehost框上运行。奇怪的是,我的网站在线运行良好。我可以拉起django / admin / site就好了。但是,我无法通过manage.py migrate运行manage.py shell或对数据库进行任何查询。这是我得到的错误:

File "/home2/univetg1/python3/lib/python3.5/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 172, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/home2/univetg1/python3/lib/python3.5/site-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

这是我在settings.py中的数据库设置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'univetg1_lunchbox',
        'USER': 'username',
        'PASSWORD': 'password',
        'HOST': '',
        'PORT': '',
    }
}

这很奇怪,因为我没有玩过我的settings.py文件或其他任何事情,直到现在才开始工作。

3 个答案:

答案 0 :(得分:1)

如果收到如下错误:

ERROR: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

然后服务器是:

  • 没有跑步;
  • 在5432以外的端口上运行;或
  • 配置了不同的unix_socket_directories路径,因此它不会在/tmp
  • 中侦听套接字

我们知道服务器正在运行,因为您的Django站点正在运行。所以这可能是连接信息的问题。假设Django实际上正在使用settings.py中的数据,它也必须在端口5432上。

所以我的猜测是它正在使用不同的unix_socket_directories。也许您在交互式控制台上在运行时链接到的libpq与运行服务时链接到的localhost不同。

您可以尝试通过将主机指定为unix_socket_directories来连接TCP / IP。否则,请检查postgresql.confHOST设置的内容,并将其设置为<script> $('#datatables').dataTable ({ "sScrollX": "200%", //This is what made my columns increase in size. "bScrollCollapse": true, "sScrollY": "800px", "bAutoWidth": false, "aoColumns": [ { "sWidth": "5%" }, // 1st column width { "sWidth": "null" }, // 2nd column width { "sWidth": "70%" }, // 3rd column width { "sWidth": "null" }, // 4th column width { "sWidth": "null" }, // 5th column width { "sWidth": "40%" }, // 6th column width { "sWidth": "null" }, // 7th column width { "sWidth": "null" }, // 8th column width { "sWidth": "null" }, // 9th column width { "sWidth": "35%" }, // 10th column width { "sWidth": "35%" }, // 11th column width { "sWidth": "null" }, //12 { "sWidth": "null" }, //13 { "sWidth": "null" }, //14 { "sWidth": "null" }, //15 { "sWidth": "null" }, //16 { "sWidth": "15%" } //17 ], "bPaginate": true, "sDom":'<"H"lCfr>t<"F"ip>', //"sDom": '<"search"f><"top"l>rt<"bottom"ip><"clear">', "sPaginationType":"full_numbers", "aaSorting":[[0, "asc"]], "bJQueryUI":true }); </script>

答案 1 :(得分:0)

您可能受到可以对数据库建立的连接数的限制,要么在conf中增加连接,要么终止某些现有连接。

我的另一个猜测是你没有使用与生产过程相同类型的django设置。因此,您应该检查您使用的设置文件(指定的或在ENV中)。

答案 2 :(得分:-1)

我遇到了与此类似但不同的解决方案。

我运行 Django wsgi 的 Gunicorn 服务器运行良好,可以访问远程数据库。当我尝试使用 python3 manage.py ... 运行任何东西时,我会收到错误消息,询问数据库是否在本地运行。我不明白为什么 python3 manage.py 试图连接到本地 Postgres 实例,但 Gunicorn 能够正常访问远程数据库。

问题:

我正在采购一个看起来像这样的 .prod.env 文件。

DB_NAME=postgres
DB_USER=myuser
DB_PASSWORD=mypassword
DB_HOST=myhost.com
DB_PORT=5432

在 Gunicorn 中,设置这些变量是因为我将此文件设置为 systemd 正在读取的 EnvironmentFile 中的 gunicorn.service

对于 python3 manage.py ... 命令,我正在执行 source .prod.env,然后直接从 shell 运行 python3 manage.py ...。当你按照我上面所做的方式定义一个变量时,它不是一个环境变量。这是一个外壳变量。它不适用于您从该 shell 生成的进程。

要定义环境变量并获取它的来源,您的环境文件应如下所示。

export DB_NAME=postgres
export DB_USER=myuser
export DB_PASSWORD=mypassword
export DB_HOST=myhost.com
export DB_PORT=5432

现在,当我运行 source .prod.env 时,这些变量是环境变量,可用于从该 shell 生成的任何进程。

2 天在 Django 的源代码中猛烈抨击我的头脑和打印语句,以此作为我的解决方案。 ?