Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql and django - Unix domain socket

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 ?

like image 426
user1407540 Avatar asked May 31 '12 07:05

user1407540


People also ask

Does Django work with PostgreSQL?

Django officially supports the following databases: PostgreSQL. MariaDB. MySQL.

Are UNIX domain sockets reliable?

Valid socket types in the UNIX domain are: SOCK_STREAM, for a stream-oriented socket; SOCK_DGRAM, for a datagram-oriented socket that preserves message boundaries (as on most UNIX implementations, UNIX domain datagram sockets are always reliable and don't reorder datagrams); and (since Linux 2.6.

Why PostgreSQL is good with Django?

Benefits of using PostgreSQL with DjangoDjango has django. contrib. postgres to make database operations on PostgreSQL. If you are building an application with maps or you are storing geographical data, you need to use PostgreSQL, as GeoDjango is only fully compatible with PostgreSQL.


2 Answers

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.

like image 182
Tometzky Avatar answered Oct 16 '22 16:10

Tometzky


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!

like image 40
Brendan Quinn Avatar answered Oct 16 '22 17:10

Brendan Quinn