我正在尝试按照Heroku上的Django教程运行,教程链接如下:
一切都很顺利,直到我遇到了syncdb部分:
当我运行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()