如何使用php通过打开的ssh隧道连接到postgres数据库?

问题描述:

我需要使用PHP连接到远程服务器上的postgres数据库。我在本地机器上打开了一个SSH隧道,本地端口8000.隧道似乎工作正常。如何使用php通过打开的ssh隧道连接到postgres数据库?

当我试图用PHP连接到数据库时出现问题。下面的代码:

$ConnectionString = "host=127.0.0.1:8000 user=username password=passphrase dbname=databasename sslmode=require port=5432"; 
pg_connect($ConnectionString); 

产生以下错误:

Warning: pg_connect(): Unable to connect to PostgreSQL server: could not translate host name "127.0.0.1:8000" to address: Unknown host in [path]\mappage.php on line 22 
Not connected 

那么,如何能我打开通过SSH数据库连接?

值得一提的是,我可以使用SSH连接到PGAdmin的远程PostgreSQL数据库。

书面in the manual它应该是:

$ConnectionString = "host=127.0.0.1 port=8000 user=username password=passphrase dbname=databasename sslmode=require"; 

你有口两次,一次不正确。

+0

不幸的是,它只是导致代码产生以下错误:'警告:pg_connect():无法连接到PostgreSQL服务器:SSL错误:未知协议在[路径] \ mappage.php在线22 未连接:' 。 – budgiebeaks