KindOfGuy
KindOfGuy

Reputation: 3211

Django Heroku postgres connectivity error

I'm trying to push a basic Wagtail Django site (pip install wagtail) to Heroku following this guide with guidance from the Heroku docs and I am getting a very common postgres connectivity error (below). I can see from the dashboard that Heroku is providing the live database, and I can access it with heroku pq:psql.

The project runs locally and also when I run heroku local.

My `project/app/settings/production.py' is set up as recommended:

import os

env = os.environ.copy()
SECRET_KEY = env['SECRET_KEY']

from __future__ import absolute_import, unicode_literals

from .base import *

DEBUG = False

try:
    from .local import *
except ImportError:
    pass

# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
print('check')
print(DATABASES)

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers
ALLOWED_HOSTS = ['*']

Procfile (at root of repo)

web: gunicorn myproj.wsgi:application --log-file -

myproj/wsgi.py

from __future__ import absolute_import, unicode_literals

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproj.settings.dev")

application = get_wsgi_application()

The error I get:

  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 189, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

Can anyone help?

Upvotes: 1

Views: 645

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599630

Your wsgi file is pointing to your dev settings; the production ones are not being used at all.

Using setdefault to set the environment variable means that an existing value will be used in preference if it is already set. So you should run heroku config:set DJANGO_SETTINGS_MODULE=myproj.settings.production to set that value.

Upvotes: 2

Related Questions