2016-12-26 45 views
1

我正在嘗試使用託管在heroku上的PostGres數據庫,我需要從本地計算機連接到它。繼承人我使用的憑據的例子:PHP - 從本地主機連接到heroku PostGres數據庫(在Windows上)

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 

當我嘗試使用Doctri連接內斯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工具欄的命令行進行連接。我可以連接pgAdmin 3.但似乎無法連接到這樣的網絡應用程序。

回答

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

UPDATE:

我沒有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 
+0

我試圖在包裝和wrotes添加空格,沒有任何改變。我在帖子中提到我也嘗試過使用DBAL,所以這對我使用'pg_connect'的方式不是問題。 DBAL從我發佈的YAML文件中獲取參數。 –

+0

讓我創建並檢查一個 – marmeladze

+0

@ HorseO'Houlihan我已經更新了我的答案。 – marmeladze

2

感謝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") 

後,我「嘗試」來修復憑證(注意密碼是不同的格式,我只好分配給了錯誤的變量),錯誤消息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 
相關問題