如何在Mac上连接PostgreSQL和PHP?

时间:2019-02-10 05:57:09

标签: php postgresql

我是PHP新手。我想使用我的PHP文件中存储在pgSQL中的数据库。请说明如何集成pgSQL和PHP,以便可以使用pg_connect()在PHP中使用pgSQL。我的php.info()显示了在PDO支持下启用的MySQL,pgSQL和SQLite。但是当我使用pg_connect()时会说:

Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: role "postgres" does not exist in /Users/username/Sites/index.php on line 3

Warning: pg_last_error(): No PostgreSQL link opened yet in /Users/username/Sites/index.php on line 4

Warning: pg_last_error(): supplied resource is not a valid PostgreSQL link resource in /Users/username/Sites/index.php on line 4
Could not connect:

我已使用本教程在Mac上运行PHP。但是我找不到一个清楚地说明如何集成PHP和pgSQL的教程。

https://www.dyclassroom.com/howto-mac/how-to-install-apache-mysql-php-on-macos-mojave-10-14

<?php
  // Connecting, selecting database
  $dbconn = pg_connect("host=localhost dbname=ipl user=postgres password=password")
      or die('Could not connect: ' . pg_last_error());

  // Performing SQL query
  $query = 'SELECT * FROM player';
  $result = pg_query($query) or die('Query failed: ' . pg_last_error());

  // Printing results in HTML
  echo "<table>\n";
  while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
      echo "\t<tr>\n";
      foreach ($line as $col_value) {
          echo "\t\t<td>$col_value</td>\n";
        }
      echo "\t</tr>\n";
  }
  echo "</table>\n";

  // Free resultset
  pg_free_result($result);

  // Closing connection
  pg_close($dbconn);
  ?>

1 个答案:

答案 0 :(得分:0)

我遇到了问题。我的用户名不是postgres,而是我的名字。我必须在安装Postgres时进行设置。因此,它说不存在作为“ postgres”的角色。