Deploying to Heroku with Postgres in Vapor 2.0

182 Views

This tutorial assumes:

  • You have a Heroku account
  • You have a vapor project with Postgresql installed, as described here

If your local postgres db is not working/persisting data with your Vapor app, you should resolve that before proceeding.

At this point under Config/secrets, you should have a postgresql.json file.

First, in your project directory, initialize a new git repository with $ git init and then $ git add . to add all files (excluding the ones in .gitignore of course). Then $ git commit -m "first commit" to commit.

Next, run:

    $ vapor heroku init

You will see a series of successive messages (with one coming after the other, after you have responded). Respond n to all.

    Would you like to provide a custom Heroku app name?
    y/n> 

    Would you like to deploy to other than US region server?
    y/n>

    Would you like to provide a custom Heroku buildpack?
    y/n>

    Are you using a custom Executable name?
    y/n>  

    Would you like to push to Heroku now?
    y/n> 

Respond with "no" to all of these.

Now, we need to add the Postgres add-on. Run:

    $ heroku addons:create heroku-postgresql:hobby-dev

Next, in your Xcode project, create a new file under the Config directory, and call it postgesql.json. This will be a separate postgresql.json than the one under the /secrets folder. You should have 2 postgresql.json files in the project now. Copy and paste the following into your new file (this postgresql.json will be checked into git):

    { "url": "$DATABASE_URL" }

This is simply to pass the app the database URL from heroku. If you navigate to Personal Apps -> Your App Name Here -> Settings -> Reveal config vars in Heroku, you will see the DATABASE_URL config variable, with its value there.

Now, in your project directory, from Finder (not in Xcode), find your Procfile.

Delete all contents from your procfile, and replace it with the following:

    web: Run --env=production --port=$PORT

Add and commit your changes to git (git add . and git commit -m "your commit message")

Then, scale your dynos on Heroku:

    $ heroku ps:scale web=1

Finally, run $ git push heroku master, and navigate to /plaintext, and you should see:

    "Hello, world!"

thanks for reading! leave questions/comments below : )



Enjoy this article? Consider supporting VaporForums by following us on Twitter! Get the latest Vapor articles, tutorials, and news.