2012-09-07 44 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 。如果我出現任何問題或者您需要其他東西,請告訴我。

相關問題