无法使用pg_connect将php连接到postgres数据库

问题描述 投票:0回答:3

我只是尝试将我的PHP代码连接到postgres数据库但不幸的是它不起作用,这是我的代码:

<?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";
   }

浏览器结果:

警告:pg_connect():无法连接到PostgreSQL服务器:致命:第11行/var/www/test/pgtest.php中不存在数据库“public”

谁知道这里有什么问题?

php database postgresql connection
3个回答
1
投票

您确定数据库的名称是“公共”吗?就像我记得的那样,'public'是PostgreSQL数据库中默认模式的名称。

请确保数据库'public'确实存在。


1
投票

从命令行,成为postgres用户,启动psql,并发出\ list命令列出您的数据库。确保您尝试打开的数据库位于列表中。

确保您的pg_hba.conf文件允许打开数据库。

从pg_connect调用中删除除$ dbname之外的所有内容,并允许API使用默认值。按错误消息指示添加参数。


0
投票

抱歉,但最后我发现我的问题是错的,publictestdbpostgres数据库中的方案,

© www.soinside.com 2019 - 2024. All rights reserved.