1

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

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.";
   }

?>

2 Comments

I solved it long time ago.. thanks the problem is you can't connect via localhost. only through heroku server
Please tell how to connect with SSL in mysqli_connect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.