postgresql - django.db.utils.OperationalError: could not connect to server: Connection refused
postgresql - django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "host_name" (XX.XX.XX.XX)
and accepting TCP/IP connections on port 5432?
尝试设置数据库服务器时的典型错误消息。但我无法修复它。
我的 Django 数据库设置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'db_name',
'USER': 'db_user',
'PASSWORD': 'db_pwd',
'HOST': 'host_name',
'PORT': '5432',
}
}
我添加到pg_hba.conf
host all all 0.0.0.0/0 md5
host all all ::/0 md5
我在postgresql.conf
中替换了:
listen_addresses = 'localhost'
到 listen_addresses = '*'
并重新启动 postgresql:
/etc/init.d/postgresql stop
/etc/init.d/postgresql start
但仍然出现同样的错误。有趣的是:
我可以从外部 ping XX.XX.XX.XX
并且它有效。但我无法远程登录:
telnet XX.XX.XX.XX
Trying XX.XX.XX.XX...
telnet: connect to address XX.XX.XX.XX: Connection refused
telnet: Unable to connect to remote host
如果我从外部远程登录端口 22,它会工作:
telnet XX.XX.XX.XX 22
Trying XX.XX.XX.XX...
Connected to server_name.
Escape character is '^]'.
SSH-2.0-OpenSSH_6.7p1 Debian-5+deb8u3
如果我从数据库服务器内部远程登录端口 5432,我会得到:
telnet XX.XX.XX.XX 5432
Trying XX.XX.XX.XX...
Connected to XX.XX.XX.XX.
Escape character is '^]'.
来自外部的相同端口:
telnet XX.XX.XX.XX 5432
Trying XX.XX.XX.XX...
telnet: connect to address XX.XX.XX.XX: Connection refused
telnet: Unable to connect to remote host
nmap 来自内部:
Host is up (0.000020s latency).
Not shown: 998 closed ports
PORT STATE SERVICE
22/tcp open ssh
5432/tcp open postgresql
来自外部的 nmap:
Starting Nmap 7.60 ( https://nmap.org ) at 2018-01-24 07:01 CET
没有回应。
听起来像是防火墙问题,但我不知道去哪里找。我做错了什么,可能是什么问题?
感谢任何帮助。
顺便说一句:我可以在服务器内登录到 postgresql,它有效:
psql -h host_name -U user_name -d db_name
psql (9.4.15)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.
db_name =>
正如我猜测的那样,问题是防火墙阻止了这些端口。我试图与托管公司沟通,但最后我不得不将服务器更改为其他托管公司,并且它使用确切的设置
Is the server running on host "host_name" (XX.XX.XX.XX)
and accepting TCP/IP connections on port 5432?
尝试设置数据库服务器时的典型错误消息。但我无法修复它。
我的 Django 数据库设置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'db_name',
'USER': 'db_user',
'PASSWORD': 'db_pwd',
'HOST': 'host_name',
'PORT': '5432',
}
}
我添加到pg_hba.conf
host all all 0.0.0.0/0 md5
host all all ::/0 md5
我在postgresql.conf
中替换了:
listen_addresses = 'localhost'
到 listen_addresses = '*'
并重新启动 postgresql:
/etc/init.d/postgresql stop
/etc/init.d/postgresql start
但仍然出现同样的错误。有趣的是:
我可以从外部 ping XX.XX.XX.XX
并且它有效。但我无法远程登录:
telnet XX.XX.XX.XX
Trying XX.XX.XX.XX...
telnet: connect to address XX.XX.XX.XX: Connection refused
telnet: Unable to connect to remote host
如果我从外部远程登录端口 22,它会工作:
telnet XX.XX.XX.XX 22
Trying XX.XX.XX.XX...
Connected to server_name.
Escape character is '^]'.
SSH-2.0-OpenSSH_6.7p1 Debian-5+deb8u3
如果我从数据库服务器内部远程登录端口 5432,我会得到:
telnet XX.XX.XX.XX 5432
Trying XX.XX.XX.XX...
Connected to XX.XX.XX.XX.
Escape character is '^]'.
来自外部的相同端口:
telnet XX.XX.XX.XX 5432
Trying XX.XX.XX.XX...
telnet: connect to address XX.XX.XX.XX: Connection refused
telnet: Unable to connect to remote host
nmap 来自内部:
Host is up (0.000020s latency).
Not shown: 998 closed ports
PORT STATE SERVICE
22/tcp open ssh
5432/tcp open postgresql
来自外部的 nmap:
Starting Nmap 7.60 ( https://nmap.org ) at 2018-01-24 07:01 CET
没有回应。
听起来像是防火墙问题,但我不知道去哪里找。我做错了什么,可能是什么问题?
感谢任何帮助。
顺便说一句:我可以在服务器内登录到 postgresql,它有效:
psql -h host_name -U user_name -d db_name
psql (9.4.15)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.
db_name =>
正如我猜测的那样,问题是防火墙阻止了这些端口。我试图与托管公司沟通,但最后我不得不将服务器更改为其他托管公司,并且它使用确切的设置