3

I'm making a Django app with the Two Scoops of Django template. Getting this Heroku error, are my Postgres production settings off?

  • OperationalError at / 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?
  • Exception Location: /app/.heroku/python/lib/python2.7/site-packages/psycopg2/__init__.py
  • foreman start works fine
  • Procfile: web: python www_dev/manage.py runserver 0.0.0.0:$PORT --noreload
  • local.py settings:

    DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'www', 'USER': 'amyrlam', 'PASSWORD': '*', 'HOST': 'localhost', 'PORT': '5432', } }

  • production.py settings: commented out local settings from above, added standard Heroku Django stuff:

    import dj_database_url DATABASES['default'] = dj_database_url.config()

    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

    ALLOWED_HOSTS = ['*']

    import os BASE_DIR = os.path.dirname(os.path.abspath(file)) STATIC_ROOT = 'staticfiles' STATIC_URL = '/static/'

    STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), )

  • UPDATE: production settings, tried changing:

import dj_database_url DATABASES['default'] = dj_database_url.config(default=os.environ["DATABASE_URL"])

(named my Heroku color URL to DATABASE_URL, same link in heroku config)

1 Answer 1

5

Have you set your DJANGO_SETTINGS_MODULE environment variable? I believe what is happening is this: by default Django is using your local.py settings, which is why it's trying to connect on localhost.

To make Django detect and use your production.py settings, you need to do the following:

heroku config:set DJANGO_SETTINGS_MODULE=settings.production

This will make Django load your production.py settings when you're on Heroku :)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, that worked for me. In wsgi.py and manage.py I've explicitly referred to DJANGO_SETTINGS_MODULE as myprojectname.settings.production. To run locally, I run python manage.py runserver --settings=myprojectname.settings.local. Does that seem like an ok workflow?
Yes, that's totally fine! That's the best practice :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.