【问题标题】:Django Postgresql syncdb errorDjango Postgresql 同步数据库错误
【发布时间】:2013-10-27 02:03:00
【问题描述】:

当我运行 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.
        }
    }

【问题讨论】:

  • 你的 /etc/hosts 中是否定义了 localhost?
  • 'nslookup localhost' 在你的机器中返回什么?
  • 您尝试只输入 127.0.0.1 还是将该行留空?
  • glasslion, localhost 在我的 etc/hosts.txt 文件中定义为:127.0.0.1 localhost
  • nslookup localhost' 返回:服务器:128.32.136.9 地址:128.32.136.9#53 ** 服务器找不到本地主机:NXDOMAIN

标签: python django localhost


【解决方案1】:

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

'HOST': ''

如果这不起作用,以下应该:

'HOST': '127.0.0.1'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    相关资源
    最近更新 更多