vincenttian
vincenttian

Reputation: 41

Django Postgresql syncdb error

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.
        }
    }

Upvotes: 4

Views: 2045

Answers (1)

Ashwin Balamohan
Ashwin Balamohan

Reputation: 3332

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'

Upvotes: 1

Related Questions