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

时间:2016-02-17 11:14:17

标签: python django postgresql

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

好吧,现在想的是我无法启动服务器,而我键入'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?

我搜索过很多关于它的文档,例如in this topic,但我找不到让它正常工作的方法。我在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

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

顺便说一下,我的数据库settings.py看起来像这样:

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

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

我非常感谢这个问题的帮助,我一直在寻找和尝试,但没有退出。非常感谢你。

编辑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?

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

6 个答案:

答案 0 :(得分:9)

对我而言,postgres没有启动和启用。所以我通过这样做解决了这个问题:

 import java.lang.*;
 import java.io.*;
 import java.util.*;

 class ReverseString
 {
     public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter String To Reverse:- ");
    String input = sc.next();    

    // convert String to character array
    char[] arr = input.toCharArray();

    for (int i = arr.length-1; i>=0; i--)
        System.out.print(arr[i]);
}
}

答案 1 :(得分:5)

我遇到了同样的问题,我忘了开始使用Postgres。从我的应用程序打开它并选择“打开psql”解决了这个问题。

答案 2 :(得分:4)

将端口默认设置为5432

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

运行命令:

python manage.py runserver 127.0.0.1:8000

答案 3 :(得分:2)

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

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

并阻止它

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

答案 4 :(得分:0)

尝试删除主机和端口:

# settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'db_name',
        'USER': 'db_user',
        'PASSWORD': 'db_pass',
    }
}

我的问题是在此处指定主机和/或端口。

答案 5 :(得分:0)

贷记到Addicted above。我检查了日志,意识到在某个时候我已经升级了Postgres版本。

$ nano server.log

  GNU nano 2.0.6                               File: server.log                                                                     

2020-12-16 02:19:32.273 EST [32414] FATAL:  database files are incompatible with server
2020-12-16 02:19:32.273 EST [32414] DETAIL:  The data directory was initialized by PostgreSQL version 12, which is not compatible with this version 13.0.

like this可以解决的问题。