If you have not deployed your Laravel project to the production then you can have a read on the other post on How to Deploy Your Laravel Website to Heroku for Free. With that of the way, let's get started and connect it right in.
Step 1 - Create Database Instance
To create a new database instance you will have to access the creation page through the menu inside the Heroku dashboard.

By default, Heroku comes with 3 types of datastores but in this post, we'll focus on "Heroku Postgres".

Do note that since it's a Heroku Add-ons, you will have to install it to have access to the service. On the page below, "Install Heroku Postgres" and then connect it with any of the Heroku applications that you have set up. If you are not deploying your application to Heroku then you can just install the Heroku Postgres on its own without associating it to any of the applications.

Step 2 - Connect Heroku Postgres with Heroku App Instance
Heroku provides several plans by default, but you can choose the "Hobby Dev" as a starter since it's free to use. On this page, you can also connect this add-on to the Heroku application but if you are planning to use it for non-Heroku deployed applications, you can just continue to create the database instance.

Once it's ready, you will be able to view the database instance from the "Resources" tab of the dashboard page.

Once again if you visit the "Heroku Datastore" page, you will be able to see the newly created PostgreSQL instance on the page.

By default, a maximum of 20 connections is allowed for the "free" plan and you can only store 10k rows of data. If you need more than that then an upgrade is required.

Step 3 - Retrieve the Credentials
To retrieve the database credentials, you can visit the "Settings" page and here you can "view all of the database credentials". You can make use of these data if you are planning to access the database from the terminal or GUI app and etc.

Do note that by default the value of the DATABASE_URL will be automatically set to the Heroku application "Config Vars" (env) previously when connecting the Heroku Postgres to the application instance. In this case, it's using the DATABASE_URL but if you prefer the granular config DB_CONNECTION, DB_HOST, DB_PORT and etc, you can define it one line at a time.

Step 4 - Run Migration
Now that we have the PostgreSQL instance connected to the Heroku application, you can run the database migration command. To access the Heroku instance you can use the Heroku CLI which you can install using "homebrew" if you are on a macOS. Otherwise, to get the CLI on windows, you will have to use the installer.
brew tap heroku/brew && brew install heroku

The console command will appear like below. You will have to type in "bash" and hit the "run" button and it will create an online bash console for you.


From the console, you can run any of the "php artisan" commands so let's instruct it to run the Laravel migration.

Run "php artisan migrate" to migrate the table migration and press "yes" to continue executing the command. The table will be migrated and now you can "php artisan tinker" and query your model to see for the result.
php artisan migrate

By now you should have the table migration migrated and you can perform CRUD operation and have the data stored inside the database. If you found this tutorial to be helpful do share it with your friends, cheers and happy coding 🍻.
Other Heroku Related Post
- Setting up Laravel Queue worker on Heroku
- Setting up a Custom Domain name and FREE SSL certificate for Heroku Apps
- How to Deploy Your Laravel Website to Heroku for Free