4

When I run python manage.py syncdb, I get this error:

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

my settings.py file looks like this:

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.
        }
    }
7
  • Is localhost defined in your /etc/hosts? Commented Oct 18, 2013 at 9:32
  • What does 'nslookup localhost' return in your machine? Commented Oct 18, 2013 at 9:34
  • you try just putting in 127.0.0.1 or leaving that line blank? Commented Oct 19, 2013 at 3:44
  • glasslion, localhost is defined in my etc/hosts.txt file as: 127.0.0.1 localhost Commented Oct 19, 2013 at 3:47
  • nslookup localhost' returns: Server: 128.32.136.9 Address: 128.32.136.9#53 ** server can't find localhost: NXDOMAIN Commented Oct 19, 2013 at 3:48

1 Answer 1

1

I've had this error multiple times. If you're trying to connect to a local database, your best bet is to leave the host as an empty string:

'HOST': ''

If that doesn't work, the following should:

'HOST': '127.0.0.1'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.