What could be the potential reasons for the "Fatal error: Call to undefined function: pg_connect()" when trying to establish a connection to PostgreSQL in PHP?
The "Fatal error: Call to undefined function: pg_connect()" error occurs when the PHP PostgreSQL extension is not enabled or installed on the server. To solve this issue, you need to make sure that the PHP PostgreSQL extension is installed and enabled in your PHP configuration.
// Check if the PostgreSQL extension is enabled
if (!extension_loaded('pgsql')) {
die('PostgreSQL extension not enabled. Please enable it in your php.ini file.');
}
// Connect to PostgreSQL database
$host = 'localhost';
$port = '5432';
$dbname = 'database';
$user = 'username';
$password = 'password';
$conn = pg_connect("host=$host port=$port dbname=$dbname user=$user password=$password");
if (!$conn) {
die('Connection failed: ' . pg_last_error());
} else {
echo 'Connected to PostgreSQL database successfully.';
}
Related questions
-
PHP sessions form data store
-
PHP security risks login functionality
-
multidimensional array category tree PHP