Answers.Body: <p>Craft/Yii on Heroku has been made much simpler with the addition of the <code>url</code> config option:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php return [
      // ...
      'production' =&gt; [
          'url' =&gt; getenv('DATABASE_URL')
      ]
      // ...
  ];
  </code></pre>
  
  <p>I think there are too many factors for us to say why the one-click deploy works, but pushing an existing application doesn't—it could be that one of the custom environment variables isn't populated (as it would be with Heroku's deploy/app wizard).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-04T04:25:29.133Z
Answers.LastActivityDate: 2020-03-04T04:25:29.133Z
Answers.UserDisplayName: August Miller
Answers.Body: <p>Found out that there are a variety of Config vars in Heroku you need to set that match your local .env file. In particular:</p>
  
  <ul>
  <li>ENVIRONMENT</li>
  <li>SECURITY_KEY</li>
  <li>DB_SCHEMA</li>
  </ul>
  
  <p><strong>Edit 10.1.19</strong> Wrote <a href="https://dev.to/coda/how-to-deploy-craft-cms-with-postgres-on-heroku-1c2h" rel="nofollow noreferrer">this tutorial</a> on how to get Craft 3 up and running on Heroku.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-30T02:12:00.337Z
Answers.LastActivityDate: 2019-10-01T23:05:06.747Z
Answers.LastEditDate: 2019-10-01T23:05:06.747Z
Answers.UserDisplayName: atc
Body: <p>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. </p>
  
  <p><strong>Background</strong></p>
  
  <p>I tried setting up Craft to work with Heroku by following <a href="https://torbensko.com/posts/2019/creating-a-site-using-craftcms-heroku-and-cloudcannon/" rel="nofollow noreferrer">this guide</a> (see the "Publishing" section near the bottom of the guide). I found that if I just run a <code>heroku create</code> and then push my local branch to the remote server, I get an "Internal Server Error" when loading the remote site on Heroku.</p>
  
  <p>Instead, I created an <code>app.json</code> file as per <a href="https://github.com/edenspiekermann/craft3-heroku-starterkit" rel="nofollow noreferrer">this guide</a> by @edenspiekermann. Then I run <code>https://heroku.com/deploy</code> with arguments to create the Heroku app, and this allows me to access the Craft admin page at /admin/install on the remote server.</p>
  
  <p>Following the <a href="https://torbensko.com/posts/2019/creating-a-site-using-craftcms-heroku-and-cloudcannon/" rel="nofollow noreferrer">first guide</a>, I also push my local Postgres database to Heroku with <code>heroku pg:push DB_NAME DATABASE_URL --app HEROKU_APP_NAME</code>.</p>
  
  <p><strong>Questions</strong></p>
  
  <p>-How does Heroku know the right <code>DATABASE_URL</code> to use in <code>/config/db.php</code>? Locally, that variables comes from <code>.env</code> which is not pushed to my repo or Heroku.</p>
  
  <p>-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 <a href="https://heroku.com/deploy" rel="nofollow noreferrer">https://heroku.com/deploy</a> works)?</p>
  
  <p>My <code>db.php</code> file:</p>
  
  <pre><code>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
  ];
  </code></pre>
  
  <p>My <code>Procfile</code> for Heroku:</p>
  
  <pre><code>web: vendor/bin/heroku-php-apache2 web/
  </code></pre>
  
  <p>My <code>.env</code> file:</p>
  
  <pre><code># 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"
  
  </code></pre>
  
  <p>Error I receive when navigating to homepage on Heroku after I've pushed my local Postgres db to Heroku remote:</p>
  
  <pre><code>HTTP 503 – Service Unavailable – craft\web\ServiceUnavailableHttpException
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-09-24T00:51:52.387Z
Comments.Text: Was able to figure out the answer to my two questions (by setting the `ENVIRONMENT` and `SECURITY_KEY` in Config Vars of the Settings dashboard in Heroku). How getting a HTTP 503 "Service Unavailable" error when navigating to the remote server :(
Comments.UserDisplayName: atc
Tags: <postgresql><heroku>
Title: How do you deploy Craft 3 on Heroku with Postgres?
ThreadText: # How do you deploy Craft 3 on Heroku with Postgres?
  
  (Asked by: username_0 on 2019-09-23)
  
  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](https://torbensko.com/posts/2019/creating-a-site-using-craftcms-heroku-and-cloudcannon/) (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](https://github.com/edenspiekermann/craft3-heroku-starterkit) 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](https://torbensko.com/posts/2019/creating-a-site-using-craftcms-heroku-and-cloudcannon/), 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' => "pgsql",
    'server' => $server,
    'user' => $user,
    'password' => $password,
    'database' => $database,
    'schema' => getenv('DB_SCHEMA'),
    'tablePrefix' => getenv('DB_TABLE_PREFIX'),
    'port' => $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
  
  ```
  
  **Tags:** postgresql, heroku
  
  > **Comments:**
  > **username_0**: Was able to figure out the answer to my two questions (by setting the `ENVIRONMENT` and `SECURITY_KEY` in Config Vars of the Settings dashboard in Heroku). How getting a HTTP 503 "Service Unavailable" error when navigating to the remote server :(  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-03-04. Score: 1
  
  Craft/Yii on Heroku has been made much simpler with the addition of the `url` config option:
  
  ```php
  <?php return [
      // ...
      'production' => [
          'url' => getenv('DATABASE_URL')
      ]
      // ...
  ];
  
  ```
  
  I think there are too many factors for us to say why the one-click deploy works, but pushing an existing application doesn't—it could be that one of the custom environment variables isn't populated (as it would be with Heroku's deploy/app wizard).
  
  
  ---
  
  ### Answer by username_0 on 2019-09-30. Score: 1
  
  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](https://dev.to/coda/how-to-deploy-craft-cms-with-postgres-on-heroku-1c2h) on how to get Craft 3 up and running on Heroku.
  
  
  ---
  
  