我在Heroku中部署了一个Django应用程序。最初,它在 SQLite3 数据库上运行,之后我升级到 PostgreSQL 。应用程序与本地设置完美配合,但是当我将文件推送到服务器时,它的行为并不像预期的那样。
当我登录django admin(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?
答案 0 :(得分:6)
Heroku依赖于数据库配置环境变量。请阅读here了解更多信息。
在你的本地环境中(你启用了虚拟环境,对吧?):
pip install dj-database-url
pip freeze > requirements.txt
然后在您的production
设置中(您的本地设置仍然可以使用localhost数据库),添加以下内容:
import dj_database_url
DATABASES = {default': dj_database_url.config()}
你知道其余的。
git add .
git commit -m 'added dj-database-url package'`
git push heroku master