• 14
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

PHP can't connect to PostgreSQL on CentOS 7

I have CentOS 7 running in VirtualBox on OSX. Apache, PHP 5.4 and PostgreSQL 9.2 are all running. But, when my (simple) php-script tries to connect to PostgreSQL it doesn't work:

Warning: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Permission denied Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432? in /var/www/html/pg.php on line 7

Checks:

  • Apache is running
  • PHP is fine, phpinfo() tells me PostgreSQL functions are available
  • PostgreSQL 9.2 is running
  • psql can connect to the database using localhost or 127.0.0.1 and also 192.168.178.111
  • pgAdmin on my Mac can connect to this database using ip address 192.168.178.111
  • iptables has been turned off
  • pg_hba.conf has been changed to accept all connections without any password (stupid, I know):

host all all 0.0.0.0/0 trust

But php can't connect.... This is my script:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

echo 'hello world!';

$conn = pg_connect('host=127.0.0.1 port=5432 user=postgres dbname=postgres');
?>

Internal connections work (psql), external connections also work (pgAdmin on a my laptop), but the php connection doesn't work...

What is going wrong? What is it that I'm missing?

Probably SELinux is blocking your database connection.

Make sure that you set the correct boolean to allow your web application to talk to the database:

sudo setsebool -P httpd_can_network_connect_db 1
  • 14
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions