Teifion
Teifion

Reputation: 111019

Python mod_wsgi database connection not working

I've installed Postgres, mod_wsgi and python3.5 on a centos system. I've tested the connection and it works fine both in the interactive Python shell as well as the development application (it's a Pyramid app so it's the dev server from that).

I'm getting the following error:

postgresql.exceptions.ClientCannotConnectError: could not establish connection to server
  CODE: 08001
  LOCATION: CLIENT
CONNECTION: [failed]
  failures[0]:
    socket('127.0.0.1', 5432)
    Traceback (most recent call last):
      File "/var/www/wsgi/lib/python3.3/site-packages/postgresql/protocol/client3.py", line 136, in connect
        self.socket = self.socket_factory(timeout = timeout)
      File "/var/www/wsgi/lib/python3.3/site-packages/postgresql/python/socket.py", line 64, in __call__
        s.connect(self.socket_connect)
    PermissionError: [Errno 13] Permission denied
    The above exception was the direct cause of the following exception:
    postgresql.exceptions.ConnectionRejectionError: Permission denied
      CODE: 08004
      LOCATION: CLIENT
CONNECTOR: [Host] pq://username:***@localhost:5432/db_name
  category: None
DRIVER: postgresql.driver.pq3.Driver

I spent some time looking at the mod_wsgi documentation and found there is a possible issue with sockets. However, I've already implemented that change (and the corresponding error from apache vanished).

My assumption is that it's a socket error still as the connection sting works fine elsewhere in the same Python install and even within the dev setup.

Can anybody point me in the right direction from here please?

Upvotes: 1

Views: 1177

Answers (1)

Graham Dumpleton
Graham Dumpleton

Reputation: 58543

Even if you set the database connect string to an IP address, if it is 127.0.0.1, I have encountered PostgreSQL database connectors which will try and optimise things and connect via the local UNIX socket instead. If the user that your code runs under with mod_wsgi cannot access that UNIX socket for accessing PostgreSQL it will fail with a permissions error.

I can't remember whether it was py-postgresql module that I saw this with, but did hit it a couple of weeks ago with on of the PostgreSQL database modules.

Try replacing 127.0.0.1 with the actual host name or proper IP address.

Upvotes: 3

Related Questions