8

i spend couple of hours to solve these problem but did not reach anything. There are several topics about this problem on the net but none of them says an absolute thing to solve this.

I just installed postgresql in order to use it on my django project.

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

this is my settings.py and the error is that

could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

Did anyone have a solution about it ?

5
  • Why your 'HOST' and 'PORT' are empty? If you running your django server locally, then you must specify at least 'HOST':'localhost' Commented May 31, 2012 at 8:01
  • What's your system and version? How did you install Postgresql - from source, from distribution package or from installer? Did you check that Postgres service is really running? Where is Postgres socket - /var/run/postgresql/.s.PGSQL.5432 or /tmp/.s.PGSQL.5432 or in some other place. Are you able to connect using psql. For a couple of hours of work you don't provide much information to work on. Commented May 31, 2012 at 8:20
  • Debian 6 . host and port is empty because i'm on localhost no need to fill it Commented May 31, 2012 at 9:01
  • postgresql is running no problem . there's no directory /var/run/postgresql/.s.PGSQL.5432 and tmp/.s.PGSQL.5432 . My postgresql is under opt directory Commented May 31, 2012 at 9:02
  • Can it be that the unix-domain socket is located in another directory (eg /tmp/) You can point the host (-h) flag to this directory instead of a hostname. BTW: /tmp/ is the default location for the socket; but some distribution packagers have chosen to put it somewhere else. sigh Commented May 31, 2012 at 11:10

3 Answers 3

17

You need to locate a postgres socket. Check main postgres process pid (ps auxw | grep postgres) and list its open unix sockets (lsof -p [PID_OF_POSTGRES_PROCESS] | grep unix). Write this path to HOST option in settings.py.

Installing Postgres from distribution package (apt-get install postgresql) would be much easier (for example empty HOST in settings.py would work) and safer as your distribution will install security updates for you.

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

3 Comments

Also, as an alternative to lsof, the standard way would be to simply look up the unix_socket_directory entry of the postgresql.conf server configuration file.
thank you. It works. apt-get install postgresql installs older versions of postgresql . What i did is that i just installed 9.1 from the official site. apt-get install postgresql locates where django host but manuel install install it in a different directory.
The need for the newest version is a common problem that most administrators prefer to handle by using backports. Among other benefits like automated upgrades, a backport of 9.1 would have avoided the library/server mismatch on paths that you had to resolve here.
12

Here's another possibility: if you installed a new version of PostgreSQL over an existing version, the installer might have assigned it a non-standard port number.

Django expects PostgreSQL to listen on port 5432. Even if you're using Unix sockets, the socket is named after the port number so this still matters!

Check /etc/postgresql/<version>/main/postgresql.conf for the line port = nnnn. If that number is not 5432, then that's your problem. Another indicator is that your socket file in /var/run/postgresql will be called something like .s.PGSQL.5433, using the non-standard port number.

To fix it, you can either edit the port number in postgresql.conf to use the default (5432), or if you need it to run on a non-standard port number, then set DATABASE_PORT = 'nnnn' in your django settings.py.

Thanks to Erik Forsberg for the pointer!

Comments

2

On contemporary Fedora systems, a private temp directory feature has been added that will cause this symptom. https://fedoraproject.org/wiki/Features/ServicesPrivateTmp

This feature causes web apps to use a tmp directory different from the system /tmp directory where the default PostgreSQL domain socked is located.

To change the systemd directive for this feature for Apache-httpd on Fedora, edit the file /usr/lib/systemd/system/httpd.service and change PrivateTmp=true to PrivateTmp=false

Alternatively, you can avoid using domain sockets to avoid this issue.

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.