我正尝试按照以下教程在 Heroku 上运行 Django:
Getting Started with Django on Heroku
在我到达 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()
最佳答案
在settings.py
中你原来的代码linked到,您的 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 为所有这些提供了默认值,但是一旦您混合和匹配来自不同来源的不同软件部分,这些默认值就不再有帮助,并且给出明确的值成为一个要求。
当对套接字的路径有疑问时,在psql
中以postgres用户连接时,可以通过SQL命令获取该路径:
SHOW unix_socket_directory;
此设置也存在于服务器端 postgresql.conf
配置文件中。
关于python - Django 与 Heroku 上的 PostgreSQL 应用程序不同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12321064/