我正在尝试按照以下教程在 Heroku 上运行 Django:
Heroku 上的 Django 入门 https://devcenter.heroku.com/articles/django
一切都运行良好,直到我到达syncbd部分:
同步数据库 https://devcenter.heroku.com/articles/django#syncing-the-database
当我跑步时:heroku run python manage.py syncdb
,我收到以下错误:
psycopg2.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"?
我目前正在使用 Homebrew 的 PostgreSQL,并且运行良好:
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
应用程序服务器也在本地运行:
Validating models...
0 errors found
Django version 1.4.1, using settings 'hellodjango.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
我正在使用 Mac OS 10.7。
我部署到 Heroku 的代码可以在这里找到:
链接到我的代码 https://dl.dropbox.com/u/2490888/heroku.zip
我已经尝试了很多可能的解决方案,例如:
http://jeffammons.net/2011/09/fixing-postgres-on-mac-10-7-tiger-for-django/ http://jeffammons.net/2011/09/fixing-postgres-on-mac-10-7-tiger-for-django/
但似乎没有任何作用。
EDIT:
环顾四周,我找到了这段代码,并将其添加到 settings.py 文件中,它似乎解决了我的问题:
# Register database schemes in URLs.
urlparse.uses_netloc.append('postgres')
urlparse.uses_netloc.append('mysql')
try:
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()