我使用Django / Pycharm连接到Windows上的Postgres数据库。我已经在其他计算机上做了很多次这样的事情,并且非常有信心我正确配置了连接。
每隔一段时间数据库连接失败并返回错误。它既发生在我的Django应用程序中,也发生在使用pycharm UI。这很奇怪,因为它至少每9/10次我与数据库进行交互,但我注意到可能它通常发生在我尝试快速连发多次与db进行交互时,但情况并非总是如此。
在pycharm数据库连接UI中,当我测试连接时,通常有效,但有时会失败。我使用默认的'postgres'用户,我怀疑这可能是问题所在。
以下是我的Django应用程序中的db配置,虽然我非常肯定这个问题与我的代码无关:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'contract_directory_site',
'USER': 'postgres',
'PASSWORD': 'my_password',
}
}
除了密码之外的所有内容都与我的代码和PyCharm数据库连接属性中的内容完全相同。
我从Django得到的错误:
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
在发出10次或更少的请求后,几乎可以保证发生这种情况,并且在第一次尝试时就已经发生了。这是整个堆栈跟踪:
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:/Users/fbgnew/PycharmProjects/fbg/procurematch_era/directory_site\listings\views.py", line 44, in listing_search
return render(request, 'listings/search_results.html', context)
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\shortcuts.py", line 30, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\template\loader.py", line 68, in render_to_string
return template.render(context, request)
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\template\backends\django.py", line 66, in render
return self.template.render(context)
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\template\base.py", line 207, in render
return self._render(context)
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\template\base.py", line 199, in _render
return self.nodelist.render(context)
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\template\base.py", line 990, in render
bit = node.render_annotated(context)
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\template\base.py", line 957, in render_annotated
return self.render(context)
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\template\defaulttags.py", line 173, in render
len_values = len(values)
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\db\models\query.py", line 232, in __len__
self._fetch_all()
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\db\models\query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\db\models\query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\db\models\sql\compiler.py", line 874, in execute_sql
cursor = self.connection.cursor()
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\db\backends\base\base.py", line 254, in cursor
return self._cursor()
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\db\backends\base\base.py", line 229, in _cursor
self.ensure_connection()
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\db\backends\base\base.py", line 213, in ensure_connection
self.connect()
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\db\backends\base\base.py", line 213, in ensure_connection
self.connect()
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\db\backends\base\base.py", line 189, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\django\db\backends\postgresql\base.py", line 176, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\fbgnew\Anaconda3\lib\site-packages\psycopg2\__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
[24/Oct/2017 16:08:14] "POST /home/ HTTP/1.1" 500 180346
也许值得注意的是我在安装postgres时遇到了一些麻烦。出于某种原因,我无法使命令行工具正常工作,我假设我的PATH搞砸了,但我的时间紧迫,只要数据库正常工作,我将在以后找出CLI。