Unable to connect to PostgreSQL server

2019-07-30 12:00发布

I'm trying to connect to the PostgreSQL database of my app on Heroku:

$host = "ec2-54-235-242-31.compute-1.amazonaws.com";
$username = "user";
$password = "pass";
$database = "dbname";
$port = "5432";

$dbconn = pg_connect("host=".$host." port=".$port
          ." dbname=".$database." user=".$username." password=".$password)
or die('Could not connect: ' . pg_last_error());

but I'm getting this error:

Warning: pg_connect() [function.pg-connect]: Unable to connect to PostgreSQL server: FATAL: no pg_hba.conf entry for host "69.196.177.196", user "user", database "dbname", SSL off in D:\wamp\www\heroku_app\test.php on line 53

What is the problem here? How can I fix it?

Note: this only happens when I run on my local machine (via WAMP server). If I deploy it to Heroku then it runs fine

3条回答
不美不萌又怎样
2楼-- · 2019-07-30 12:33

Add "sslmode=require;" to your connection string.

查看更多
该账号已被封号
3楼-- · 2019-07-30 12:35

Check this quick tip for further help, but, briefly, you obtain your credentials using:

heroku pg:credentials COLOR

... where COLOR is part of the return value from:

heroku addons:add heroku-postgresql

which doubles as the command to add postgres to your application.

查看更多
祖国的老花朵
4楼-- · 2019-07-30 12:45

The key error here is SSL off. Heroku Postgres requires SSL for external connections. Make sure your PHP was compiled with SSL and specify it in your connection sslmode=require.

查看更多
登录 后发表回答