How do you deploy Craft 3 on Heroku with Postgres? I'm relatively new to Craft 3, and am able to run Craft locally with a Postgres db and Valet as my server. When I try to deploy to Heroku, I run into issues with (I think) Postgres. 

Background

I tried setting up Craft to work with Heroku by following this guide (see the "Publishing" section near the bottom of the guide). I found that if I just run a heroku create and then push my local branch to the remote server, I get an "Internal Server Error" when loading the remote site on Heroku.

Instead, I created an app.json file as per this guide by @edenspiekermann. Then I run https://heroku.com/deploy with arguments to create the Heroku app, and this allows me to access the Craft admin page at /admin/install on the remote server.

Following the first guide, I also push my local Postgres database to Heroku with heroku pg:push DB_NAME DATABASE_URL --app HEROKU_APP_NAME.

Questions

-How does Heroku know the right DATABASE_URL to use in /config/db.php? Locally, that variables comes from .env which is not pushed to my repo or Heroku.

-Why does Heroku show an internal server error if I just push my local branch to the remote server (whereas creating a Heroku app with https://heroku.com/deploy works)?

My db.php file:

preg_match('|postgres://([a-z0-9]*):([a-z0-9]*)@([^:]*):([0-9]*)/(.*)|i', getenv('DATABASE_URL'), $matches);

$user = $matches[1];
$password = $matches[2];
$server = $matches[3];
$port = $matches[4];
$database = $matches[5];

return [
  'driver' =&gt; "pgsql",
  'server' =&gt; $server,
  'user' =&gt; $user,
  'password' =&gt; $password,
  'database' =&gt; $database,
  'schema' =&gt; getenv('DB_SCHEMA'),
  'tablePrefix' =&gt; getenv('DB_TABLE_PREFIX'),
  'port' =&gt; $port
];


My Procfile for Heroku:

web: vendor/bin/heroku-php-apache2 web/


My .env file:

# The environment Craft is currently running in ('dev', 'staging', 'production', etc.)
ENVIRONMENT="dev"

# The secure key Craft will use for hashing and encrypting data
SECURITY_KEY=""

# The database driver that will be used ('mysql' or 'pgsql')
DB_DRIVER="pgsql"

# The database server name or IP address (usually this is 'localhost' or '127.0.0.1')
DB_SERVER="localhost"

# The database username to connect with
DB_USER="atc"

# The database password to connect with
DB_PASSWORD=""

# The name of the database to select
DB_DATABASE="blogdb"

# The database schema that will be used (PostgreSQL only)
DB_SCHEMA="public"

# The prefix that should be added to generated table names (only necessary if multiple things are sharing the same database)
DB_TABLE_PREFIX=""

# The port to connect to the database with. Will default to 5432 for PostgreSQL and 3306 for MySQL.
DB_PORT="5432"

DATABASE_URL="postgres://atc:@localhost:5432/blogdb"

DEFAULT_SITE_URL="https://landing-page.dev"



Error I receive when navigating to homepage on Heroku after I've pushed my local Postgres db to Heroku remote:

HTTP 503 – Service Unavailable – craft\web\ServiceUnavailableHttpException
Found out that there are a variety of Config vars in Heroku you need to set that match your local .env file. In particular:


ENVIRONMENT
SECURITY_KEY
DB_SCHEMA


Edit 10.1.19 Wrote this tutorial on how to get Craft 3 up and running on Heroku.