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
How to Add a Condition Inside a PHP Array
Best Way to Handle Dirty State in an Orm Model
PHP MySQL SQL Parser (Insert and Update)
Difference Between Echo and Return in PHP
Warning: File_Get_Contents: Failed to Open Stream: Redirection Limit Reached, Aborting
How to Get the Last Dir from a Path in a String
How to Solve "Non-Static Method Xxx:Xxx() Should Not Be Called Statically in PHP 5.4
Send Email by Email Class in Codeigniter with Gmail
How to Get the List of Available Locales in PHP
How to Integrate PHPmailer with Codeigniter 3
How to Disable PHP Magic Quotes at Runtime
Any PHP Code to Detect the Browser with Version and Operating System
Call PHP from JavaScript and Return an Array from PHP to JavaScript Function
Rewrite All Queries to Not Need the .PHP Extension Using a Mod_Rewrite Rewriterule