4

当我运行 python manage.py 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.
        }
    }
4

1 回答 1

1

我多次遇到此错误。如果您尝试连接到本地数据库,最好的办法是将主机保留为空字符串:

'HOST': ''

如果这不起作用,则应执行以下操作:

'HOST': '127.0.0.1'

于 2013-11-15T04:41:32.747 回答