问题
I am unsuccesfully getting my django app deployed on heroku to use my local postgres db.
My DATABASE settings are as follows:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER': 'foo',
'PASSWORD': 'bar',
'HOST': 'localhost',
'PORT': '',
}
}
Everything runs fine locally. Following the instructions from https://devcenter.heroku.com/articles/django, I add the following bit a code to the bottom of my settings file:
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://foo:bar@localhost/mydb')}
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
However, this produces the following error:
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?
My django app runs fine on Heroku, except when it needs to connect to the database, which is where it throws this error.
Anyone know what I am doing wrong here?
回答1:
- Allow remote postgres access to your local db. example
- Change the settings file to something like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'your db name',
'USER': 'your username',
'PASSWORD': 'password',
'HOST': 'Your computer's IP',
'PORT': '6122',
}
Or you have to clone your local db to heroku postgres and change your settings to use that db.
回答2:
Simply put, your current configuration defines the database host as localhost
while you should be pointing it at your hosts public IP (or fully qualified domain name).
But this seems like an anti-pattern. You really should be using Heroku's development databases for this stuff, and if you have any local data you'd like to import there, just make a database dump and load it.
来源:https://stackoverflow.com/questions/17778422/deploying-django-apps-local-postgres-database-to-heroku