Recently I had the same problem but luckily for both I could solve it, then I leave everything I have done to work with databases in postgres;
Personally I use WAMP server and manage the connection with PDO and its methods;
The first thing is to open the file php.ini
and within the file find the line “extension=php_pdo_pgsql.dll”
which will have a semicolon in front (;)
, this is removed and the wamp
is restarted. This to use PDO with postgres
To make the connection you can take the following code as an example;
file: dbconfig_postgres.php
<?php
$host='127.0.0.1';
$db = 'mi_base_datos';
$username = 'postgres';
$password = '1706';
?>
file: conexion_postgres.php
<?php
require_once 'dbconfig_postgres.php';
$dsn = "pgsql:host=$host;port=5432;dbname=$db;user=$username;password=$password";
try{
$base_postgres = new PDO($dsn);
$base_postgres->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//echo "Conexion correcta PostGres";
}catch (PDOException $e){
echo "BD fuera de servicio";
}
?>
already configured the connection is only to make queries, I leave you an example;
include('conexion_postgres.php');
$registros=$base_postgres->prepare('Select id,nombre,correo From "mi_esquema"."mi_tabla"');
$registros->execute(array());
//Por si quieres saber cuantos registros arrojo la consulta
$registros = $registros_niscap->rowCount();
$registros = $registros->fetchAll( PDO::FETCH_OBJ );
Assuming that you know how to use PDO, the rest is accessed through an indexed array or with a foreach.
Anyway, any question I can continue editing the answer to add what you need.