1

I'm new in this cpanel and I want to ask how to connect to Postgres in cpanel using php?

I use this simple code

<?php

$dbconn = pg_connect("host=localhost port=5432 dbname=test user=domain_test password=test")
or die('connection failed: ' . pg_last_error());

?>

and It keep on returning connection failed on my browser, can somebody tell me how to do it correctly?

5
  • And what's the error? (see how to get it here - php.net/manual/en/pgsql.examples-basic.php) Commented Jan 23, 2014 at 11:48
  • the . pg_last_error() contains nothing... its empty.. sorry.. i will include it right away... Commented Jan 23, 2014 at 11:51
  • Check the php and/or Postgres' logs. Commented Jan 23, 2014 at 11:57
  • i have checked the php logs, and did not find anything... regarding to postgres log, i didn't find the file in my cpanel... Commented Jan 23, 2014 at 12:10
  • @Diom Look on the file system - it's usually in /var/lib/postgresql or /var/lib/pgsql, but if you're on the kind of hosting system that has CPanel, anything goes... Commented Jan 23, 2014 at 12:21

1 Answer 1

1

You can't really catch connection errors with pg_last_error. You will need to use pg_connection_status for that. But it will not give you enough info to take care of the connection issue.

It looks like error reporting is disabled in your case. So give this a try

error_reporting(E_ALL);
ini_set('display_errors', true);

$dbconn = pg_connect("host=localhost port=5432 dbname=test user=domain_test password=test");

$stat = pg_connection_status($dbconn);
if ($stat === PGSQL_CONNECTION_OK) {
    echo 'Connection status ok';
} else {
    echo 'Connection status bad';
}
Sign up to request clarification or add additional context in comments.

1 Comment

Alright I got it, I just need to change the host to host=127.0.0.1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.