Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

python - Deploying Django to Heroku (Psycopg2 Error)

So I'm following the getting started guide from heroku 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 this meant that the db wasn't set up yet... so I manually added the shared_db option as well:

heroku addons:add shared-database:5mb

But.. I still get the same error. What gives?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

EDITED:

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

import dj_database_url

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

This works if you have a DATABASE_URL env variable set. heroku:pg_promote gets your there. Details below


Make sure you have Postgres on your Heroku

heroku addons:add heroku-postgresql:dev

Step 1: figure out your database url

heroku config | grep POSTGRESQL

The output will look something like this:

HEROKU_POSTGRESQL__URL: postgres://user:password@host:5432/blabla

Step 2: Grab the setting name from the previous step (e.g. HEROKU_POSTGRESQL_ROSE_URL) and put it in your settings file like so

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

[UPDATE] As Ted has pointed out, there's a way to promote the color URL to DATABASE_URL variable:

heroku pg:promote HEROKU_POSTGRESQL_ROSE_URL

Your database settings can then use DATABASE_URL as opposed to more exotic colored URLS

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

Bob is your uncle


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...