PHP - 从本地主机连接到 heroku PostGres 数据库(在 Windows 上)

PHP - Connecting to heroku PostGres database from localhost (on Windows)

我正在尝试使用托管在 heroku 上的 PostGres 数据库,我需要从我的本地计算机连接到它。这是我使用的凭据示例:

params:
  driver: pdo_pgsql
  host: e5-31-281-414-28.compute-1.amazonaws.com
  dbname: b6m44bwjmaa41n
  user: sgjwiakskki
  password: e4ks28ajgs87
  port: 5432

^^ 我更改了这些凭据,但这就是它们的格式。

当我尝试使用 pg_connect 连接时,如下所示:

$dbconn = pg_connect("e5-31-281-414-28.compute-1.amazonaws.com port=5432 dbname=b6m44bwjmaa41n user=sgjwiakskki password=e4ks28ajgs87") or die('Could not connect: ' . pg_last_error());

我收到这个错误:

pg_connect(): Unable to connect to PostgreSQL server: could not translate host name "***.compute-1.amazonaws.com" to address: Unknown host 

当我尝试使用 Doctrines DBAL 包进行连接时,我得到了同样的错误:

Uncaught exception 'PDOException' with message 'SQLSTATE[08006] [7] could not translate host name "e5-31-281-414-28.compute-1.amazonaws.com" to address: Unknown host' in C:\wamp64\www\project\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\AbstractPostgreSQLDriver.php on line 85

我阅读了所有关于从外部连接到 heroku 的教程,但我无法理解这一点。我可以使用 heroku toolbelt 通过命令行连接。我可以连接 pgAdmin 3。但似乎无法连接这样的网络应用程序。

如手册所述,您应该尝试某事。如下所示。

http://php.net/manual/en/function.pg-connect.php

$dbconn = pg_connect("host=e5-31-281-414-28.compute-1.amazonaws.com port=5432 dbname=b6m44bwjmaa41n user=sgjwiakskki password=e4ks28ajgs87");

Description
resource pg_connect ( string $connection_string [, int $connect_type ] )

The connection_string can be empty to use all default parameters, or it can contain one or more parameter settings separated by whitespace. Each parameter setting is in the form keyword = value. Spaces around the equal sign are optional. To write an empty value or a value containing spaces, surround it with single quotes, e.g., keyword = 'a value'. Single quotes and backslashes within the value must be escaped with a backslash, i.e., \' and \.

更新:

我没有 php。所以,我刚刚从旧的 ubuntu 存储库安装了 php5.6 和 php5.6-pqsql 软件包(我使用的是 Linux Mint 18.1、32 位)

$ date
Tue Dec 27 00:49:11 +04 2016
$ grep install /var/log/dpkg.log

2016-12-27 00:05:11 install php-common:all <none> 1:47+deb.sury.org~xenial+1
...
2016-12-27 00:05:22 install php5.6:all <none> 5.6.29-1+deb.sury.org~xenial+1
...
2016-12-27 00:22:26 status installed php5.6-pgsql:i386 5.6.29-1+deb.sury.org~xenial+1
... 

然后在 heroku 上创建了帐户。设置一个数据库,我正在添加连接凭据(因为我不需要它)。

Host
    ec2-54-217-212-8.eu-west-1.compute.amazonaws.com
Database
    d464heusrdn2k5
User
    mgtvhefjhzilai
Port
    5432
Password
    fd6aa97d31b25c26a41a8211008f1838abd76d7715dcac7c742bfc55f37bd50f

以上给出,

$ php -v
PHP 5.6.29-1+deb.sury.org~xenial+1 (cli) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

$ php -a
Interactive mode enabled

php > $dbconn = pg_connect("host=ec2-54-217-212-8.eu-west-1.compute.amazonaws.com port=5432 dbname=d464heusrdn2k5 user=mgtvhefjhzilai password=fd6aa97d31b25c26a41a8211008f1838abd76d7715dcac7c742bfc55f37bd50f");
php > $sql = "create table lorem (id int not null, foo varchar(15), primary key (id));";
php > $r = pg_query($dbconn, $sql);
php > $tbls = "select * from information_schema.tables";
php > $qr = pg_query($dbconn, $tbls);
php > while($r = pg_fetch_array($qr)) {
php { print_r($r);
php { }
Array
(
    [0] => d464heusrdn2k5
    [table_catalog] => d464heusrdn2k5
    [1] => public
    [table_schema] => public
    [2] => lorem
    [table_name] => lorem
    [3] => BASE TABLE
    [table_type] => BASE TABLE
    [4] => 
    [self_referencing_column_name] => 
    [5] => 
    [reference_generation] => 
    [6] => 
    [user_defined_type_catalog] => 
    [7] => 
    [user_defined_type_schema] => 
    [8] => 
    [user_defined_type_name] => 
    [9] => YES
    [is_insertable_into] => YES
    [10] => NO
    [is_typed] => NO
    [11] => 
    [commit_action] => 
)
Array

谢谢marmeladze!通过像那样走出你的路并向我展示它对你有用,我可以看出问题很可能是我使用了错误的凭据。原来有两个问题,错误的凭据是第一个。第二个是我需要将 sslmode=require 放入 $connection_string。 所以字符串变成了这样:

$dbconn = pg_connect("host=ec2-**-***-***-45.compute-1.amazonaws.com port=5432 dbname=e4ngs3d4risaw9 user=s52mt674s3sjd password=p31mgss562sts7e3kebie3j sslmode=require")

在我 "attempted" 修复凭据后(注意密码是不同的格式,我分配了错误的变量),错误消息 unable to resolve host 错误消失,并被替换两个新的错误。第一个 oen 是:

Unable to connect to PostgreSQL server: FATAL: password authentication failed for user

和第二个我忘记保存了,但它说了一些关于提供的用户名没有出现在 pg_hba.conf 文件中,用于提供的主机(它为主机提供了一个 IP)。我尝试编辑 pg_hba.conf 文件,但后来发现一篇关于常见错误消息及其含义的文章: https://devcenter.heroku.com/articles/postgres-logs-errors#fatal-no-pg_hba-conf-entry-for-host-user-u-database-d-ssl-off 这意味着您正在尝试不使用 SSL 进行连接。

FATAL: no pg_hba.conf entry for host "…”, user “u…”, database “d…”, SSL off

You are attempting to establish a database connection without SSL. SSL is required for all connections. Please see the documentation for your postgres driver on how to establish encrypted connections.

所以添加 sslmode=require 修复了那个问题。然后身份验证问题显然是密码问题,我仔细检查了一下,密码中缺少一个字母。修复解决了所有问题,现在我可以连接了。再次感谢!

UPDATE:只是想添加我遇到的另一个错误及其原因。我尝试改用 DBAL 连接,但出现此错误:

PDOException' with message 'SQLSTATE[08006] [7] FATAL: password authentication failed for user "SYSTEM"' in C:\wamp64\www\myproject\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\AbstractPostgreSQLDriver.php on line 85

它正在尝试连接 SYSTEM 用户。问题是我的 YAML 文件,它有 username: hs62ksi62sj2,而它应该是 user: hs62ksi62sj2

所以工作的 YAML 文件是:

params:
  driver: pdo_pgsql
  host: ec2-**-***-***-45.compute-1.amazonaws.com
  dbname: e4ngs3d4risaw9 
  user: s52mt674s3sjd 
  password: p11mgss562sts7e3kebiesf
  port: 5432
  sslmode: require