2012-09-07 43 views
4

我试图在Heroku上运行的Django的教程如下:与Django的Heroku上PostgreSQL的应用程序是否未同步

Getting Started with Django on Heroku

一切都运行良好,直到我到了syncbd部分:

Syncing the database

当我运行: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"? 

我目前使用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的代码可以在这里找到:

Link to my code

我已经尝试了很多可能的解决方案,如:

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() 

回答

1

在你linked来,看来你有你的DATABASES设置两个相互矛盾的声明中的原代码settings.py

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配置文件。

+0

我很新,感谢您解释它! – Filipe

0

它可能是你不在数据库中加载你的PORT。你的代码看起来像加载数据库连接的代码是什么?

+0

我正在使用'pg_ctl start' – Filipe

+0

这是命令行。 Python代码在我给出的链接中可用。 – Filipe

1

我刚刚部署和Django应用程序的Heroku中与posgresql,这是我的代码,它完美的作品,我希望它能帮助:

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环境变量。这将是这个样子: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 。如果我出现任何问题或者您需要其他东西,请告诉我。

相关问题