Unable to connect to PostgreSQL server

▼魔方 西西 提交于 2020-01-15 15:26:04

问题


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


回答1:


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.

  • Heroku Postgres: External connections (ingress)



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/16351253/unable-to-connect-to-postgresql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!