Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I just try to connect my php code to postgres database but unfortunately it is not work, here is my code :

<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
   $host        = "host=127.0.0.1";
   $port        = "port=5432";
   $dbname      = "dbname=public";
   $credentials = "user=postgres password=postgres";

   $db = pg_connect( "$host $port $dbname $credentials"  ) or die('Could not connect');;
   if(!$db){
      echo "Error : Unable to open database\n";
   } else {
      echo "Opened database successfully\n";
   }

browser result :

Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: database "public" does not exist in /var/www/test/pgtest.php on line 11

anybody know what is wrong here ?

share|improve this question

Are you sure the name of database is 'public'? As fas as I remember 'public' is a name of default schema inside the PostgreSQL database.

Please make sure the database 'public' really exists.

share|improve this answer
    
I also tried to connect testdb that just I created and it is still gives same error: Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: database testdb does not exist in /var/www/test/pgtest.php on line 11 – Yusuf Err Found Jul 13 '14 at 18:50

From the command line, become the postgres user, start psql, and issue the \list command to list your databases. Be sure the database you're trying to open is in the list.

Be sure your pg_hba.conf file allows the database to be opened.

Remove everything except $dbname from the pg_connect call and allow the API to use the defaults. Add parameters back as indicated by the error message(s).

share|improve this answer

sorry but finally I found that my question was wrong, public and testdb are Schemes inside postgres database,

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.