简体   繁体   中英

could not connect to server:PostgreSQL -Heroku

I deployed a Django app in Heroku. Initially, it was run on SQLite3 database and later I upgraded to PostgreSQL . App is working perfect with local settings but, when I pushed the file to server it doesn't behave as expected.

When I login to django admin ( https://my_app_name.herokuapp.com/admin/ ) got an error as below

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?

Can anyone solve this issue ? Thanks

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'myproject',
        'USER': 'myprojectuser',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '',
    }
}

Traceback

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?

Heroku depends on databse configuration environment variables. Read here for more.

In you local environment do (you have a virtual env enabled, right?):

  1. pip install dj-database-url
  2. pip freeze > requirements.txt

Then in your production settings (your local settings can still use the localhost db), add these:

  1. import dj_database_url
  2. DATABASES = {default': dj_database_url.config()}

You know the rest.

git add .
git commit -m 'added dj-database-url package'`
git push heroku master

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM