尝试在 Windows Server 2016 上使用 IIS 管理器 10 和 httpPlatformHandler 1.2 托管带有 PostgreSQL 数据库的 Django 4 应用程序。
在 VM 服务器上使用python manage.py runserver
cmd 运行时,应用程序和数据库工作正常。尝试使用 IIS 访问时出现问题,日志文件显示:
psycopg.OperationalError: connection failed: :1), port 23825 failed: could not receive data from server: Socket is not connected (0x00002749/10057)
could not send SSL negotiation packet: Socket is not connected (0x00002749/10057)
在我将数据库地址从 localhost 更改为 127.0.0.1 后,错误变为
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\psycopg\connection.py", line 729, in connect
raise ex.with_traceback(None)
django.db.utils.OperationalError: connection failed: could not receive data from server: Socket is not connected (0x00002749/10057)
could not send SSL negotiation packet: Socket is not connected (0x00002749/10057)
有时
psycopg.OperationalError: connection failed: could not receive data from server: Socket is not connected (0x00002749/10057)
could not send SSL negotiation packet: Socket is not connected (0x00002749/10057)
我认为 IIS 中有一些选项可以阻止建立连接。
答案1
如果您还没有这样做,您可能需要更新您的“pg_hba.conf”文件,以允许通过 IPv4 和 IPv6 从本地主机进行连接。
#TYPE DATABASE USER ADDRESS METHOD
host all all 127.0.0.1/32 trust
host all all localhost trust
host all all ::1/128 trust
您可以在此处找到更多信息。 https://www.postgresql.org/docs/current/auth-pg-hba-conf.html