Can't Connect to Postgresql with PHP Pg_Connect()

Can't Connect to PostgreSQL with PHP pg_connect()

could not connect to server: Permission denied

Edit: I'm on Centos 5.4 btw.

Check /var/log/audit/audit.log. Chances are that you're hitting a SELinux rule.

Unable to connect to Postgres via PHP but can connect from command line and PgAdmin on different machine

OK... Answered... Was a problem with SELinux. Needed to run the following....

setsebool -P httpd_can_network_connect_db on

Also if you need to check if SELinux is causing issues it can be turned off with the following

setenforce 0

Then once finished

setenforce 1

Anyways, done... onwards!

PHP can't connect to PostgreSQL on CentOS 7

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

How do I start a PGSQL connection in PHP?

In your pgsql connection you have missed the port number.
Try this way.

<?php
$host = "host=xxx.xx.xxx.21";
$port = "port=5432";
$dbname = "dbname=d106";
$credentials = "user=b16 password=bran";

$connect= pg_connect( "$host $port $dbname $credentials" ) or die("Could not connect: " . pg_last_error());

$result = pg_query($connect,"SELECT distinct thestartgeom FROM bikes");
if (!$result)
{
echo "no results ";
}

while($row = pg_fetch_array($result))
{

$coor = $row['thestartgeom'];
echo $coor;

}

pg_close($connect);
?>

You can store PgSQL connection code in one PHP file to reuse

pgsql_db_connection.php file

<?php
$host = "host=xxx.xx.xxx.21";
$port = "port=5432";
$dbname = "dbname=d106";
$credentials = "user=b16 password=bran";

$connect= pg_connect( "$host $port $dbname $credentials" );
if(!$connect){
echo "Error : Unable to open database\n";
}
?>

Call pgsql_db_connection.php file in other php files to use your database connection.

<?php
require_once('pgsql_db_connection.php');

$result = pg_query($connect,"SELECT distinct thestartgeom FROM bikes");
if (!$result)
{
echo pg_last_error($connect);
exit;
}

while($row = pg_fetch_array($result))
{

$coor = $row[0];
echo $coor;

}
?>

Unable to connect to PostgreSQL server with pg_connect on localhost

What is the current configuration for pg_hba.conf? And did you set a password for the database rol "postgres" ? su - postgres is for the Linux user "postgres", not for the database role "postgres". If you can start psql without entering the password, it looks like pg_hba.conf uses "trust" and not "password" of even better "md5". Or did you create a .pgpass file?



Related Topics



Leave a reply



Submit