I can not connect to postgreSQL in php

1

I'm trying to make a Login system in PHP using PostgreSQL however, I can not get my application to connect to the Database. follow the code:

 <?php
     $connect = pg_connect("dbname=testebd"); //Banco de dados previamente criado
     if (!$connect)
     {
        echo "Conexão não realizada";
     } else 
     {
        echo "Conexão bem sucedida";
     }
?>

When I run my application, absolutely nothing happens. The page is completely blank. I can not even print the contents of the variable:

$connect = pg_connect("dbname=testebd");
echo "$connect";

Someone knows tell me why the connection is not being performed ?. Note: I use the Apache server in an Ubuntu and I can usually use postegreSQl by bash.

    
asked by anonymous 26.11.2015 / 03:57

3 answers

1

1 Activate your PHP error messages:

Open this file with your preferred editor: /etc/php5/apache2/php.ini

Look for this line:

display_errors = Off

and switch to this:

display_errors = On

After this, restart apache (on the terminal):

service apache2 restart

Based on this, get the error message and put it here.

But here's an example of a connection to postgres:

<?php
if(!@($conexao=pg_connect ("host=HOST dbname=BANCO port=5432 user=LOGIN password=SENHA"))) {
   print "Não foi possível estabelecer uma conexão com o banco de dados.";
} else {
   pg_close ($conexao);
   print "Conexão OK!"; 
}
?>
    
26.11.2015 / 04:12
0

It will not connect in this way, it is necessary to inform the full dsn, with host, user and password in addition to the database.

$connect = pg_connect("host=localhost dbname=teste user=usario password=senha");
    
26.11.2015 / 04:12
-2

The PHP version 7.1 is unstable and buggy (more than 30 error pages in the php site). One of these errors is that PHP can not see the modules needed to run the pg_connect or pgsql PDO commands. And it is not a simple configuration of the php.ini file, but an internal error. To connect with postgresql you need to go back to version 5.x.

    
26.01.2017 / 20:11