gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:56:30 26 4
gpt4 key购买 nike

我正在尝试使用托管在 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

关于PHP - 从本地主机连接到 heroku PostGres 数据库(在 Windows 上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41334266/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com