无法连接到服务器:PostgreSQL -Heroku
Posted
技术标签:
【中文标题】无法连接到服务器:PostgreSQL -Heroku【英文标题】:could not connect to server:PostgreSQL -Heroku 【发布时间】:2017-08-02 08:25:12 【问题描述】:我在 Heroku 中部署了一个 Django 应用程序。最初,它在 SQLite3 数据库上运行,后来我升级到 PostgreSQL。应用程序在本地设置下完美运行,但是当我将文件推送到服务器时,它的行为与预期不符。 当我登录 django 管理员 (https://my_app_name.herokuapp.com/admin/) 时出现如下错误
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
谁能解决这个问题?谢谢
settings.py
DATABASES =
'default':
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproject',
'USER': 'myprojectuser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
追溯
Environment:
Request Method: POST
Request URL: https://cyc-new.herokuapp.com/admin/login/?next=/admin/
Django Version: 1.9
Python Version: 2.7.13
Installed Applications:
['rest_framework',
'background_task',
'userApp',
'imageUpload',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/admin/sites.py" in login
407. return login(request, **defaults)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/views.py" in inner
49. return func(*args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper
76. return view(request, *args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/views.py" in login
69. if form.is_valid():
File "/app/.heroku/python/lib/python2.7/site-packages/django/forms/forms.py" in is_valid
161. return self.is_bound and not self.errors
File "/app/.heroku/python/lib/python2.7/site-packages/django/forms/forms.py" in errors
153. self.full_clean()
File "/app/.heroku/python/lib/python2.7/site-packages/django/forms/forms.py" in full_clean
363. self._clean_form()
File "/app/.heroku/python/lib/python2.7/site-packages/django/forms/forms.py" in _clean_form
390. cleaned_data = self.clean()
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/forms.py" in clean
159. password=password)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in authenticate
74. user = backend.authenticate(**credentials)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/backends.py" in authenticate
17. user = UserModel._default_manager.get_by_natural_key(username)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/base_user.py" in get_by_natural_key
45. return self.get(**self.model.USERNAME_FIELD: username)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
122. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py" in get
381. num = len(clone)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py" in __len__
240. self._fetch_all()
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
1074. self._result_cache = list(self.iterator())
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
52. results = compiler.execute_sql()
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
850. cursor = self.connection.cursor()
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/base/base.py" in cursor
231. cursor = self.make_debug_cursor(self._cursor())
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/base/base.py" in _cursor
204. self.ensure_connection()
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/base/base.py" in ensure_connection
199. self.connect()
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/utils.py" in __exit__
95. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/base/base.py" in ensure_connection
199. self.connect()
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/base/base.py" in connect
171. self.connection = self.get_new_connection(conn_params)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/postgresql/base.py" in get_new_connection
175. connection = Database.connect(**conn_params)
File "/app/.heroku/python/lib/python2.7/site-packages/psycopg2/__init__.py" in connect
164. conn = _connect(dsn, connection_factory=connection_factory, async=async)
Exception Type: OperationalError at /admin/login/
Exception Value: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
【问题讨论】:
它对你有用吗? 【参考方案1】:Heroku 依赖于数据库配置环境变量。阅读here了解更多信息。
在您的本地环境中(您启用了虚拟环境,对吗?):
pip install dj-database-url
pip freeze > requirements.txt
然后在你的production
设置中(你的本地设置仍然可以使用localhost db),添加这些:
import dj_database_url
DATABASES = default': dj_database_url.config()
其余的你都知道。
git add .
git commit -m 'added dj-database-url package'`
git push heroku master
【讨论】:
我应该为此目的使用任何特定的 URI/URL 吗? 没有。它会自动为你处理一切! 但是,当我按照你所说的那样更改设置时,它显示另一个错误为relation "auth_user" does not exist LINE 1: ...user"."is_active", "auth_user"."date_joined" FROM "auth_user...
也许你错过了heroku run python manage.py makemigrations
和heroku run python manage.py migrate
以上是关于无法连接到服务器:PostgreSQL -Heroku的主要内容,如果未能解决你的问题,请参考以下文章
无法连接到服务器 - 错误“/var/run/postgresql/.s.PGSQL.5432”?
无法连接到 PostgreSQL 服务器:致命:角色“apache”不存在