我正在尝试按照以下教程运行Herang上的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
app服务器也在运行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()
答案 0 :(得分:1)
在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
配置文件中。
答案 1 :(得分:1)
我刚刚使用posgresql在Heroku中部署了Django应用程序,这是我的代码,它完美无缺,我希望它有所帮助:
requirements.txt
Django==1.7.4
dj-database-url==0.3.0
dj-static==0.0.6
gunicorn==19.2.1
psycopg2==2.6
six==1.9.0
static3==0.5.1
wsgiref==0.1.2
wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<PROJECT_NAME>.settings")
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())
Procfile
web: gunicorn <PROJECT_NAME>.wsgi
确保你的heroku上有Postgres:
heroku addons:add heroku-postgresql:dev
找出数据库url env变量。它看起来像这样:HEROKU_POSTGRESQL__URL
heroku config | grep POSTGRESQL
settings.py
import dj_database_url
POSTGRES_URL = "HEROKU_POSTGRESQL_<DB_NAME>_URL"
DATABASES = {'default': dj_database_url.config(default=os.environ[POSTGRES_URL])}
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['*']
# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
之后我只需将所有内容推送到主服务器并运行syncdb
heroku run python manage.py syncdb
这可能会有所帮助Getting Started with Django on Heroku。如果我出了什么问题,或者你还需要别的东西,请告诉我。
答案 2 :(得分:0)
您可能没有在数据库中加载PORT。你的代码加载数据库连接是什么样的?