1

I keep running into the problem of not being able to connect to the database:

psycopg2.OperationalError: could not connect to server: No such file or directory

Is the server running locally and accepting connections on Unix domain socket “/tmp/.s.PGSQL.5432”?

I did some reading and it seems like it’s a quite common problem that most people fix by checking that the port is correct, ie from changing 5432 to 5433 in their setup.py. But this does not seem to be my problem – in the /opt/bitnami/postgresql directory i see .s.PGSQL.5432. I can log into psql and it seems to work correctly. I think the default database name is postgresql, but i also tried it with the one called djangostack and it didn’t work either. My database information in Django's setting.py looks like this:

DATABASES = {
'default': {
    'ENGINE': 'postgresql_psycopg2', #'django.db.backends.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': 'postgres',                      # Or path to database file if using sqlite3.
    'USER': 'postgres',                      # Not used with sqlite3.
    'PASSWORD': 'bitnami',                  # Not used with sqlite3.
    'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
    'PORT': '5432',                      # Set to empty string for default. Not used with sqlite3.
}

I look in the /tmp/ directory and there isn’t anything there, hidden or otherwise. Is there supposed to be? Does anyone know what I’m doing wrong? I stupidly overwrote the original example project so I can’t see what the settings were there.

Thanks a lot, Alex

Alex S
  • 4,726
  • 7
  • 39
  • 67
  • Sorry I should add that I'm running the Bitnami Django 1.3 AMI on ec2, as seen at the bottom of [this link](http://bitnami.org/stack/djangostack). – Alex S May 23 '11 at 19:55
  • Did you install postgresql? Did you start it? Can you connect to it with a client like pgsql or pgAdmin? – Eelke May 23 '11 at 20:10
  • Yes, it is installed by default with the AMI, and I can connect to it with psql. – Alex S May 23 '11 at 21:58

3 Answers3

3

So the PostgreSQL socket is in /opt/bitnami/postgresql but your libpq (the C library that psycopg2 wraps) is looking for the socket in /tmp, right? Try changing the HOST setting to point at the proper socket location:

'default': {
    'ENGINE':   'postgresql_psycopg2',
    'NAME':     'postgres',
    'USER':     'postgres'
    'PASSWORD': 'bitnami',
    'HOST':     '/opt/bitnami/postgresql/.s.PGSQL.5432', # <-------
    'PORT':     '5432',
}

Or this:

'default': {
    'ENGINE':   'postgresql_psycopg2',
    'NAME':     'postgres',
    'USER':     'postgres'
    'PASSWORD': 'bitnami',
    'HOST':     '/opt/bitnami/postgresql', # <-------
    'PORT':     '5432',
}

And you might want to change the the PostgreSQL password too.

Community
  • 1
  • 1
mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • 1
    According to [Django docs](http://docs.djangoproject.com/en/dev/ref/settings/#std:setting-DATABASES) the socket-as-hostname feature only works for MySQL. Set HOST to 127.0.0.1 instead to bypass sockets. – Mel May 23 '11 at 20:40
  • @Mel: The documentation often presents a simplified version of reality. I found a few examples that indicate that socket-as-hostname also works with psycopg2. The docs seem to be a little ambiguous about what happens when you use a path for the host with PostgreSQL. – mu is too short May 23 '11 at 20:55
  • 1
    Thanks mu, setting "host" to "/opt/bitnami/postgresql" fixed it. – Alex S May 23 '11 at 22:02
  • If you are using the BitNami AMI setting 'HOST': '127.0.0.1', should also work. – kaysa Aug 25 '11 at 10:01
1

Change the Hostname to localhost

   DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.postgresql_psycopg2",
            "NAME": "DB",
            "USER": "USER",
            "PASSWORD": "PASS",
            "HOST": "localhost",
            "PORT": "5432",
        }
    }

Also, don't connect it with default postgres user. Create a new one.

ivewor
  • 11
  • 1
  • 4
0

Restart postgresql by using the command

sudo /opt/bitnami/ctlscript.sh restart postgresql

it worked for me

enter image description here

Jasir
  • 653
  • 8
  • 17