Problem with postgres database connection in php

Asked

Viewed 1,888 times

1

I’m making a connection to bank postgres but there’s been an error I can’t determine, following the codes:

Config.php:

define('DB','sistema_postgres');
define('HOST','localhost');
define('USER','postgres');
define('PASS','123');

Database.php:

require_once 'config.php';
class Database {
   private $db;
   private $host;
   private $user;
   private $pass;
   private $conn;

   public function __construct(){
     $this->host = HOST;
     $this->db = DB;
     $this->user = USER;
     $this->pass = PASS;
     $this->conn = new PDO("pgsql:host={$this->host};port=5432;dbname={$this->db};user={$this->user};password={$this->pass}");
   }

   protected function getConn(){
     return $this->conn;
   }
}

Turmadao.php

require_once('database.php');
class TurmaDAO extends Database implements IDAO{
    private $turma;
    private $db;

    public function __construct($turma=null) {
        if(isset($turma)){
            $this->setTurma($turma);
        }
        parent::__construct();
        $this->db = parent::getConn();
    }

    public function listAll() {
        $stmt = $this->db->prepare('SELECT * FROM turma');
        $stmt->execute();
        try{
            $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
        }
        catch(PDOException $e){
            echo $e->getMessage();
            die();
        }
        return $rs;
    } 
}

php class.

require_once 'TurmaDAO.php';
$t = new TurmaDAO();
$rs = $t->listAll();
var_dump($rs);

The following error message is being returned:

Fatal error: Uncaught Exception 'Pdoexception' with message 'SQLSTATE[08006] [7] FATAL: password Authentication failed for user "SYSTEM"'

Anyway, it is an error with the connection data, the strange thing is that the database user defined in my application is 'postgres' and something is changing the user to 'SYSTEM'

What may be causing this error ?

  • Before making the connection check which user is set on the connection

  • 1

    If you take a look at PDO documentation you will see that the username and password should be passed as parameters to the constructor, not within the dsn string. Try to change $this->conn = new PDO("pgsql:host={$this->host};port=5432;dbname={$this->db};user={$this->user};password={$this->pass}"); for $this->conn = new PDO("pgsql:host={$this->host};port=5432;dbname={$this->db};", $this->user, $this->pass);

  • tayllan, important remark, corrected here, however, is not the source of the error.

  • 1

    @tayllan the DSN of postgres is different from the DSN of Mysql you can pass everything in the first argument if you want, see in manual

  • @rray true, did not know. vlw by heads up.

  • In the __Construct database do a print_r($this); this before the new PDO(...) see what comes back and puts it there

  • @rray made the following code change: instead of directly setting the connection data I created the methods setters and encapsulated them as private. This solved the problem, however not yet say why the connection data was being modified.

  • But in print_r some value changed? came the so 'SYSTEM'?

  • @rray strangely no, the mistake was happening during the inheritance... maybe it’s some problem with my apache, well, I can’t say. I’ll run some tests here to find out. If you find out I’ll post a comment here

Show 4 more comments

1 answer

0

How is your pg_hba.conf?

You may need to exchange ident authentication for md5

# IPv4 local connections:
host    all         all         127.0.0.1/32          md5
# IPv6 local connections:
host    all         all         ::1/128               md5

Browser other questions tagged

You are not signed in. Login or sign up in order to post.