Postgresql and django - Unix domain name

I spend a couple of hours to solve this problem, but have not achieved anything. There are several topics about this problem on the net, but not one of them speaks of an absolute essence to solve this problem.

I just installed postgresql to use it in 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. } } 

these are my .py settings 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"? 

Anyone have a solution?

+6
source share
3 answers

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

Installing Postgres from the distribution ( apt-get install postgresql ) will be much simpler (for example, an empty HOST in settings.py will work) and safer, since your distribution will install security updates for you.

+15
source

Here is another possibility: if you installed a new version of PostgreSQL on top of an existing version, the installer could assign it a non-standard port number.

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

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

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

Thanks to Eric Forsberg for the pointer!

+11
source

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

This function forces web applications to use the tmp directory other than the system / tmp directory, which hosts the default PostgreSQL domain.

To change the systemd directive for this Apache-httpd function to 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 problem.

+2
source

Source: https://habr.com/ru/post/917076/


All Articles