我正在尝试按照以下教程在 Heroku 上运行 Django:
在我进入 syncbd 部分之前,一切都运行良好:
当我运行: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 的代码在这里可用:
我已经尝试了很多可能的解决方案,例如:
http://jeffammons.net/2011/09/fixing-postgres-on-mac-10-7-tiger-for-django/
但似乎没有任何效果。
编辑:
环顾四周,我找到了这段代码,并将其添加到 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()