0

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 ?

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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
1

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).

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.