2013-10-18 105 views
4

當我運行python manage.py執行syncdb,我得到這個錯誤:Django的PostgreSQL的執行syncdb錯誤

OperationalError: could not translate host name "localhost" to address: nodename nor servname provided, or not known 

我的settings.py文件看起來是這樣的:

if "IS_STAGING" in os.environ or "IS_PRODUCTION" in os.environ: 
    import dj_database_url 
    DATABASES = {'default':dj_database_url.config(default='postgres://localhost')} 
else: 
    DATABASES = { 
     'default': { 
      'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 
      'NAME': 'test',      # Or path to database file if using sqlite3. 
      # The following settings are not used with sqlite3: 
      'USER': 'test', 
      'PASSWORD': 'test', 
      'HOST': 'localhost',      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP. 
      'PORT': '',      # Set to empty string for default. 
     } 
    } 
+0

localhost是在/ etc/hosts中定義的嗎? –

+0

'nslookup localhost'在您的機器中返回什麼? –

+0

你試着把127.0.0.1放在空白處還是留空? –

回答

1

我有這個錯誤多次。如果你試圖連接到本地數據庫,你最好的辦法是離開主機爲空字符串:

'HOST': ''

如果不工作,應如下:

'HOST': '127.0.0.1'