Django deployment for Heroku (Psycopg2 bug)

So, I follow the guide to getting started with the hero with django. However, when I run this command:

heroku run python manage.py syncdb 

I get this error

 psycopg2.OperationalError: could not connect to server: Connection refused Is the server running on host "localhost" and accepting TCP/IP connections on port 5432? 

I assumed that this means that db is not configured yet ... so I manually added the shared_db parameter:

 heroku addons:add shared-database:5mb 

But I still get the same error. What gives?

+8
python django postgresql deployment heroku
source share
5 answers

My application structure is disabled ... heroku wants the structure to look like this:

 toplevel requirements.txt myapp manage.py all other django stuff 
+1
source share

Edition:

As @mipadi pointed out here (http://stackoverflow.com/questions/13001031/django-heroku-settings-injection/13092534), it can be as simple as this:

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

This works if you have the enb variable DATABASE_URL set. heroku: pg_promote gets yours there. Details below


Make sure you have Postgres on your Heroku

 heroku addons:add heroku-postgresql:dev 

Step 1: provide the URL of your database

 heroku config | grep POSTGRESQL 

The result will look something like this:

HEROKU_POSTGRESQL__URL: Postgres: // user: password @ host: 5432 / blab

Step 2: Take the parameter name from the previous step (for example, HEROKU_POSTGRESQL_ROSE_URL) and put it in your settings file this way

 DATABASES = {'default': dj_database_url.config(default=os.environ["HEROKU_POSTGRESQL_ROSE_URL"])} 

[UPDATE] As Ted pointed out, there is a way to push a colored URL into the DATABASE_URL variable:

 heroku pg:promote HEROKU_POSTGRESQL_ROSE_URL 

You can use DATABASE_URL in your database settings, unlike more exotic color URLs.

 DATABASES = {'default': dj_database_url.config(default=os.environ["DATABASE_URL"])} 

Bob is your uncle

+8
source share

I got it by adding the following code to settings.py myself, for some reason Heroku didn't add it for me ....

Usually he always added the code to Heroku dynamically, but I think that after django 1.4 for some reason he did not do this for some reason. Or I was just doing something wrong.

In any case, this is code, just add it to your settings.py settings, and it should work as before.

 import sys import urlparse import os # Register database schemes in URLs. urlparse.uses_netloc.append('postgres') urlparse.uses_netloc.append('mysql') try: # Check to make sure DATABASES is set in settings.py file. # If not default to {} if 'DATABASES' not in locals(): DATABASES = {} if 'DATABASE_URL' in os.environ: url = urlparse.urlparse(os.environ['DATABASE_URL']) # Ensure default database exists. DATABASES['default'] = DATABASES.get('default', {}) # Update with environment configuration. DATABASES['default'].update({ 'NAME': url.path[1:], 'USER': url.username, 'PASSWORD': url.password, 'HOST': url.hostname, 'PORT': url.port, }) if url.scheme == 'postgres': DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2' if url.scheme == 'mysql': DATABASES['default']['ENGINE'] = 'django.db.backends.mysql' except Exception: print 'Unexpected error:', sys.exc_info() 
+2
source share

I had the same problem, that's how I solved it

Step 1: Follow the Phillip step. Step 1 to get the database name (color)

Step 2:

 $ heroku pg:promote HEROKU_POSTGRESQL_<COLOR> 

leads to the conclusion

 Promoting HEROKU_POSTGRESQL_<COLOR> to DATABASE_URL... done 
+1
source share

You need to add this to your requirements.txt file:

 psycopg2 

By default, Heroku sets up the Postgres database and enters the code into your settings.py (https://devcenter.heroku.com/articles/django#postgres_database_config). This is read from the DATABASE_URL environment variable, but requires the installation of psycopg2.

0
source share

All Articles