Brian Cox
Brian Cox

Reputation: 75

Calling python/psycopg2 from php - getting permission denied errors connecting to the database

I've got what is probably (hopefully?) a simple permissions issue that I have been bashing my head against. Hoping some fresh eyes will help.

I'm running on Centos 6.2. I am using python to successfully access and manage a postgresql database.

However, when I try to run these python scripts via php, I get errors accessing the database.

My php call:

        exec('python test_get_bcas.py 2>&1 1> /dev/null', $output);

and my result (pardon the formatting);

  0 => 'Traceback (most recent call last):',
  1 => '  File "test_get_bcas.py", line 3, in <module>',
  2 => '    response = getBCAs()',
  3 => '  File "/var/www/html/proj/getBCAs.py", line 22, in getBCAs',
  4 => '    database = Database()',
  5 => '  File "/var/www/html/proj/databaseFunctions.py", line 32, in __init__',
  6 => '    self.connection = psycopg2.connect("dbname=bsr user=gbtc password=broadcast host=localhost port=5432")',
  7 => '  File "/usr/lib/python2.6/site-packages/psycopg2-2.4.5-py2.6-linux-x86_64.egg/psycopg2/__init__.py", line 179, in connect',
  8 => '    connection_factory=connection_factory, async=async)',
  9 => 'psycopg2.OperationalError: could not connect to server: Permission denied',
  10 => '   Is the server running on host "localhost" and accepting',
  11 => '   TCP/IP connections on port 5432?',
  12 => '',
  13 => 'Exception AttributeError: AttributeError("\'NoneType\' object has no attribute \'close\'",) in <bound method Database.__del__ of <databaseFunctions.Database instance at 0x7f0de346efc8>> ignored',

My python code looks like:

self.connection = psycopg2.connect("dbname=proj user=user password=pw host=localhost port=5432")

I think the problem is with the user that the httpd process is running as. But I have tried with messing with permissions on the files, and even adding apache as a root user in the sudoers file.

Anyone have any ideas what I'm missing??

Upvotes: 1

Views: 848

Answers (1)

Brian Cox
Brian Cox

Reputation: 75

Solved it! It was an SELinux setting that needed to be changed, specifically httpd_can_network_connect.

Doing this:

setsebool -P httpd_can_network_connect on

fixed the problem.

Upvotes: 2

Related Questions