简体   繁体   中英

cannot connect php to postgres database using pg_connect

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 ?

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.

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

抱歉,但最终我发现我的问题是错误的, publictestdbpostgres数据库中的Schemes,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM