score:0

first, you have to create a connection string from the heroku database_url environment variable.

function pg_connection_string_from_database_url() {
  extract(parse_url($_env["database_url"]));
  return "user=$user password=$pass host=$host dbname=" . substr($path, 1); # <- you may want to add sslmode=require there too
}

in order to establish a connection with the db, you can use pg_connect() which is a built-in function in php.

$conn_str = pg_connection_string_from_database_url();
$pg_conn = pg_connect($conn_str);

after successfully connecting to the db, you can run the queries as you need.

$result = pg_query($pg_conn, "select relname from pg_stat_user_tables where schemaname='public'");

few examples on how to use postgresql can be found here in official docs

full code i used for this answer can be found here


Related Query

More Query from same tag