Deploying with a Database

Full credit: Much of this is adapted from the Heroku guide, but set up to show you how to transition a local project into a heroku deployable project.

Connecting your Heroku app to a database

Heroku provides production grade PostgreSQL databases as a service.

Provision the Postgres Add-on

Add-ons are third-party cloud services that provide out-of-the-box additional services for your application, from persistence through logging to monitoring and more. By default, Heroku stores 1500 lines of logs from your application. However, it makes the full log stream available as a service - and several add-on providers have written logging services that provide things such as log persistence, search, and email and SMS alerts.

In this step you will provision one of these logging add-ons, to set up a postgres database. To add one, type this in the terminal window

heroku addons:create heroku-postgresql

You’ll see a confirmation like this:

Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pg:copy
Created postgresql-convex-90661 as HEROKU_POSTGRESQL_GREEN_URL
Use heroku addons:docs heroku-postgresql to view documentation

The add-on is now deployed and configured for your application. You can list add-ons for your app like so:

heroku addons

Listing the config vars for your app will display the URL that your app is using to connect to the database, DATABASE_URL:

heroku config

and you’ll see

=== polar-inlet-4930 Config Vars
DATABASE_URL:                postgres://xx:yyy@host:5432/d8slm9t7b5mjnd
HEROKU_POSTGRESQL_GREEN_URL: postgres://xx:yyy@host:5432/d8slm9t7b5mjnd
…

Heroku also provides a pg (postgres) command that shows a lot more:

heroku pg
=== HEROKU_POSTGRESQL_GREEN_URL (DATABASE_URL)
Plan:        Hobby-dev
Status:      Available
Connections: 0
PG Version:  9.3.3
Created:     2014-07-07 11:30 UTC
Data Size:   6.6 MB
Tables:      2
Rows:        1/10000 (In compliance)
Fork/Follow: Unsupported
Rollback:    Unsupported
This indicates I have a hobby database (free), running Postgres 9.3.3, with a single row of data.

Deploy and set up the database

Add the changes to the git repo and push to Heroku:

  1. Open GitHub Desktop and commit any new changes (type a name for the commit, and press ‘Commit to Master’, then Sync on the top right )

  2. Once it’s synced, return to the Terminal/Command Prompt window and type the following to deploy your code:

 git push heroku master

Now we’ve got our code there, we’ll need to actually set up our database

If you have migrations, you can run them like so:

heroku run rake db:migrate

If you have a seed file, you can add data to your database like so:

heroku run rake db:seed

And, you can also interact directly with the database if you have Postgres installed locally. For example, here’s how to connect to the database using psql and execute a query:

heroku pg:psql

Then you can type in SQL queries to view, insert, delete or update records:

d8slm9t7b5mjnd=> select * from widgets;
 id |   name    | description  | stock |         created_at         |         updated_at
----+-----------+--------------+-------+----------------------------+----------------------------
  1 | My Widget | It's amazing |   100 | 2014-07-08 15:05:13.330566 | 2014-07-08 15:05:13.330566
(1 row)

All done

If you’ve followed these steps, you’ve hopefully got a working project on Heroku!

Type:

heroku open

to see the live application and interact with it.

×

Subscribe

The latest tutorials sent straight to your inbox.