1

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

Chin
  • 19,717
  • 37
  • 107
  • 164
  • This is a FAQ, I'm surprised you didn't find details on this with a simple Google search like: http://google.com/search?q="pg_hba.conf+entry+for+host"+heroku – Craig Ringer May 03 '13 at 06:53
  • That's the first thing I did, but I couldn't find the solution. Which link do you think has the solution? – Chin May 03 '13 at 12:59
  • Pretty much all of them show that the issue is that you need to connect with SSL. That said, Google shows different people different results, so you could well be seeing something totally different to me. – Craig Ringer May 04 '13 at 08:08

3 Answers3

3

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.

catsby
  • 11,276
  • 3
  • 37
  • 37
0

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.

hd1
  • 33,938
  • 5
  • 80
  • 91
  • I already obtained it, and using the same credential I am able to connect to the database using pgAdmin. It just doesn't work when I use PHP – Chin May 03 '13 at 04:39
0

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

Rizon
  • 1,516
  • 4
  • 25
  • 45