2016-02-17 442 views
9

我终于(想)成功安装了PostgreSQL并且还安装了psycopg2(我使用的是Windows)。顺便说一句,有一种方法来检查它是否正常工作?无法在Django上运行服务器(连接被拒绝)

好了,想现在是我无法启动服务器,当我键入“蟒蛇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,但没有更改错误消息。出了什么问题?非常感谢你。

+4

Django开发服务器默认运行在8000端口上。为什么你为PostgreSQL服务器选择相同的端口?只需保留默认值(5432),然后重试。连接应该没问题,不要触摸任何配置文件。 – Selcuk

+0

它是否必须位于特定的目录或其他内容?试过但无法弄清楚为什么错误信息仍然存在...非常感谢。 – Jim

回答

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 
+0

现在,消息错误包括:无法连接到服务器:连接被拒绝(0x0000274D/10061) 服务器是否在主机“localhost”(127.0.0.1)上运行并且接受端口5432上的TCP/IP连接? 我应该改变记事本文件中的内容吗?谢谢你们的答案。 – Jim

+2

'python manage.py runserver'试试这个 或者'python manage.py runserver 127.0.0.1:8001' –

+0

嗨,谢谢你的回答。我运行该命令并更改了端口,但错误消息仍然存在。连接拒绝仍然发生。我编辑了第一篇文章,解释更改端口时会发生什么。我应该在配置文件中进行一些更改吗?我正在尝试一切,但不知道有什么问题......再次感谢大家 – Jim

3

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

1

如果您正在使用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 
0

对我来说是不被启动的Postgres并启用。所以我这样解决了这个问题:

sudo systemctl start postgresql 
sudo systemctl enable postgresql 
相关问题