B.Nabeih
B.Nabeih

Reputation: 171

How to connect to heroku postgres database?

I'm trying to connect to postgres databse on heroku but it doesn't work for me.

This is the code in index.php

<?php
$host = "my host";
$dbname = "my database name";
$user = "my username";
$password = "my password";
$port = "5432";

$dsn = "pgsql:host=$host;dbname=$dbname;user=$user;port=$port;password=$password";

$db = new PDO($dsn);

if($db){
  echo "Connected <br />".$db;
}else {
  echo "Not connected";
}
 ?>

But nothing appear on the screen it should print not connected if the credentials are wrong.

Help Please

Thanks all

Upvotes: 1

Views: 3384

Answers (2)

user7612612
user7612612

Reputation:

Try next code, it works with me.

<?php

   $con = "dbname=fgsfg10pdq host=ghfghfh4654.amazonaws.com port=5432 user=gafasduyiu password=435346af8493196 sslmode=require";


   if (!$con) 
   {
     echo "Database connection failed.";
   }
   else 
   {
     echo "Database connection success.";
   }

?>

Upvotes: 0

Verance
Verance

Reputation: 127

You need to get your DATABASE_URL variable. You can get it going here and press 'View Credentials'. Then fill your DB connection with the info provided.

Regards

Upvotes: 2

Related Questions