Python 无法在 Django 上运行服务器(连接被拒绝)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35455109/
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-08-19 16:27:57  来源:igfitidea点击:

Can't run the server on Django (connection refused)

pythondjangopostgresql

提问by Jim

I finally (think) installed successfully PostgreSQL and also de psycopg2 (I use Windows). Btw, is some way to check it's working properly?

我终于(认为)成功安装了 PostgreSQL 和 de psycopg2(我使用 Windows)。顺便说一句,有什么方法可以检查它是否正常工作?

Well, the thing now is that I can't start the server, while I type 'python manage.py runserver' it shows this (at the end of the command):

好吧,现在的问题是我无法启动服务器,当我输入“python manage.py runserver”时,它显示了这一点(在命令的末尾):

conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 8000?
could not connect to server: Connection refused (0x0000274D/10061)
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 8000?

I've searched a lot of documentation about it, for example in this topic, but I can't find the way to make it work properly. I've tried several changes in the pg_hba and postgresql files, but with no exit. In this moment, pg_hba looks like:

我已经搜索了很多关于它的文档,例如在这个主题中,但我找不到让它正常工作的方法。我在 pg_hba 和 postgresql 文件中尝试了几次更改,但没有退出。此时,pg_hba 看起来像:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1            md5
# IPv6 local connections:
host    all             all             127.0.0.1            md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres        ::1/128                 md5

And postgresql conf looks like:

postgresql conf 看起来像:

# - Connection Settings -

listen_addresses = 'localhost'      # what IP address(es) to listen on;
                # comma-separated list of addresses;
                # defaults to 'localhost'; use '*' for all
                # (change requires restart)
port = 8000             # (change requires restart)
max_connections = 100           # (change requires restart)
# Note:  Increasing max_connections costs ~400 bytes of shared memory per
# connection slot, plus lock space (see max_locks_per_transaction).
#superuser_reserved_connections = 3 # (change requires restart)
#unix_socket_directories = ''   # comma-separated list of directories
                # (change requires restart)
#unix_socket_group = ''         # (change requires restart)
#unix_socket_permissions = 0777     # begin with 0 to use octal notation
                # (change requires restart)
#bonjour = off              # advertise server via Bonjour
                # (change requires restart)
#bonjour_name = ''          # defaults to the computer name

Btw, my settings.py of the database look like this:

顺便说一句,我的数据库 settings.py 如下所示:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'database1',                      
    'USER': 'root',
    'PASSWORD': '123456',
    'HOST': 'localhost',
    'PORT': '8000',
    }
}

I haven't created a database BTW, how should I do it? What are the applications of the PostgreSQL prompt?

我还没有创建数据库 BTW,我该怎么做?PostgreSQL 提示符有哪些应用?

I would highly appreciate help with this issue I've been days searching and trying but with no exit. Thank you very much.

我非常感谢在这个问题上的帮助我已经搜索和尝试了好几天但没有退出。非常感谢。

EDIT 1:I tried changing the settings.py port to 5432, but now the error message is the same, just changing the port:

编辑 1:我尝试将 settings.py 端口更改为 5432,但现在错误消息相同,只需更改端口:

could not connect to server: Connection refused (0x0000274D/10061)
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?

The config files are right this way? Should I change something? I can't find an answer. I tried with python manage.py runserverand both indicating the 127.0.0.1:8000and 8001, but no changes in the error message. What's going wrong? Thank you very much.

配置文件是这样吗?我应该改变什么吗?我找不到答案。我尝试使用python manage.py runserver和 都指示127.0.0.1:8000和 8001,但错误消息没有变化。怎么了?非常感谢。

回答by Usman Maqbool

Make port default to 5432

使端口默认为 5432

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'database1',                      
    'USER': 'root',
    'PASSWORD': '123456',
    'HOST': 'localhost',
    'PORT': '5432',
    }
}

run the command:

运行命令:

python manage.py runserver 127.0.0.1:8000

回答by scollaco

I just had the same problem and I just had forgotten to start my Postgres. Opening it from my Applications and selecting "Open psql" solved the problem.

我刚刚遇到了同样的问题,只是忘记启动我的 Postgres。从我的应用程序打开它并选择“打开 psql”解决了这个问题。

回答by Addicted

If you are using Postgresql and developing on a mac then you can use the below command to start Postgresql and it should fix your problem.

如果您使用 Postgresql 并在 mac 上开发,那么您可以使用以下命令启动 Postgresql,它应该可以解决您的问题。

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

and to stop it

并阻止它

pg_ctl -D /usr/local/var/postgres stop -s -m fast

回答by Alex Jolig

For me it was for postgres not being started and enabled. So I solved the problem by doing so:

对我来说,这是因为没有启动和启用 postgres。所以我通过这样做解决了这个问题:

sudo systemctl start postgresql
sudo systemctl enable postgresql