我正在尝试按照以下教程在Heroku上运行Django:
Getting Started with Django on Heroku
一切都运行良好,直到我进入syncbd部分:
当我运行:heroku运行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
应用服务器也在运行localy:
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()
最佳答案 在您
linked到的原始代码中的settings.py中,您的DATABASES设置似乎有两个相互矛盾的声明:
1)第3行:
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
2)第16行:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'traineeworld', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
3)此外,您最新编辑的附加代码看起来是另一种指定连接参数的方法,这可能会再次否定先前声明的影响.
这些方法并不意味着相互堆叠.你只想选择一个.
此外,从技术上讲,作为客户端连接到数据库服务器的发起者,您应该知道是否要通过TCP(在这种情况下是主机名或IP地址加端口)或通过Unix域套接字文件,在这种情况下是它的完整目录路径(以斜杠开头).在这两种情况下,这都进入连接参数的HOST部分.
Postgres为所有这些提供默认值,但只要您混合和匹配来自不同源的不同软件部分,这些默认值就不再有用,并且提供显式值成为必需.
当对socket的路径有疑问时,在作为postgres用户连接的psql内部时,可以通过SQL命令获取此路径:
SHOW unix_socket_directory;
此设置也存在于服务器端postgresql.conf配置文件中.