django无法连接到服务器:权限被拒绝服务器是否在主机上运行并接受端口远程主机上的TCP / IP连接

时间:2021-07-21 18:57:54

I have a weird problem. I can connect to a remote host using psql from the command line but can't using the exact same credentials in a django settings file. I get the error:

我有一个奇怪的问题。我可以从命令行使用psql连接到远程主机,但不能在django设置文件中使用完全相同的凭据。我收到错误:

Could not connect to server: Permission denied Is the server running on host "remote ip" and accepting TCP/IP connections on port port_number?

无法连接到服务器:权限被拒绝服务器是否在主机“remote ip”上运行并接受端口port_number上的TCP / IP连接?

If I can connect using psql and not using django what am I missing here? I also checked through python interpreter that I can load psycopg2.

如果我可以使用psql连接而不使用django我在这里缺少什么?我还检查了python解释器,我可以加载psycopg2。

Any help is greatly appreciated as non of what I've found online has helped.

任何帮助都非常感谢,因为我在网上找到的并没有帮助。

Cheers,

干杯,

D

d

Here's the db conf in settings.py

这是settings.py中的db conf

'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', 
        'NAME': '<db name goes here>', # Or path to database file if using sqlite3.
        'USER': '<db user>', # Not used with sqlite3.
        'PASSWORD': '<pswd goes here>', # Not used with sqlite3.
        'HOST': '<remote ip goes here>', 
        'PORT': '5432', 
}

3 个解决方案

#1


8  

Based on your description this has something to do with the Apache and it's permissions to access the database. You didn't mention which OS you use but if it is running SELinux the default rules prevent apache from connecting to db. If this is the case you can temporarily enable it by running:

根据您的描述,这与Apache及其访问数据库的权限有关。您没有提到您使用的操作系统,但如果它正在运行SELinux,则默认规则会阻止apache连接到db。如果是这种情况,您可以通过运行以下命令暂时启用它:

setsebool httpd_can_network_connect_db

Here are more detailed instructions.

这里有更详细的说明。

#2


5  

To make changes persistent across reboots (tested on Centos):

要在重新启动后进行更改(在Centos上测试):

setsebool -P httpd_can_network_connect_db on

read more here

在这里阅读更多

#3


0  

Tested on CentOs.

在CentOs上测试。

service httpd stop

service postgresql stop

setsebool -P httpd_can_network_connect 1

service httpd start

service postgresql start

#1


8  

Based on your description this has something to do with the Apache and it's permissions to access the database. You didn't mention which OS you use but if it is running SELinux the default rules prevent apache from connecting to db. If this is the case you can temporarily enable it by running:

根据您的描述,这与Apache及其访问数据库的权限有关。您没有提到您使用的操作系统,但如果它正在运行SELinux,则默认规则会阻止apache连接到db。如果是这种情况,您可以通过运行以下命令暂时启用它:

setsebool httpd_can_network_connect_db

Here are more detailed instructions.

这里有更详细的说明。

#2


5  

To make changes persistent across reboots (tested on Centos):

要在重新启动后进行更改(在Centos上测试):

setsebool -P httpd_can_network_connect_db on

read more here

在这里阅读更多

#3


0  

Tested on CentOs.

在CentOs上测试。

service httpd stop

service postgresql stop

setsebool -P httpd_can_network_connect 1

service httpd start

service postgresql start