What steps should be taken to configure PHP to access a PostgreSQL database?

To configure PHP to access a PostgreSQL database, you need to make sure that the PostgreSQL extension is enabled in your PHP configuration file (php.ini). You also need to install the PostgreSQL database driver for PHP. Once these steps are completed, you can use the pg_connect function to establish a connection to the PostgreSQL database.

<?php
// Enable the PostgreSQL extension in php.ini
// extension=pgsql

// Connect to the PostgreSQL database
$conn = pg_connect("host=localhost dbname=mydatabase user=myuser password=mypassword");

// Check if the connection is successful
if (!$conn) {
    echo "Failed to connect to PostgreSQL database";
} else {
    echo "Connected to PostgreSQL database successfully";
}
?>