问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/6102232/cant-connect-to-postgres-database-with-bitnami-django-stack