In the last two posts, we got Laravel up and running on Heroku. But Heroku natively prefers PostgreSQL. In this article, you will find How to Install Laravel on Heroku - Using a PostgreSQL database.
You'll find that PostgreSQL can do everything on MySQL. Let's get it running on our Laravel Heroku app.
You'll find that PostgreSQL can do everything on MySQL. Let's get it running on our Laravel Heroku app.
Adding PostgreSQL to Heroku app
First, navigate to your app directory and add PostgreSQL as a Heroku add-on.
If you want to add PostgreSQL from Heroku, you can also add from https://data.heroku.com.
The environment variable for your PostgreSQL database URL has a COLOR variable in the name itself:
Depending on the server you're on, that color may be different.
That means you can't necessarily rely on the name of that environment variable always being the same.
So, you want to be sure to not rely on the HEROKU_POSTGRESQL_COLOR_URL for your database configurations.
Read on for how to handle it instead.
At any point, you can find both the name of your PostgreSQL variable and its value by running the following:
You should see something like the following:
On apps with multiple databases, or if you didn't get the DATABASE_URL set properly for some reason, you can promote a particular server to be the primary database:
heroku pg:promote HEROKU_POSTGRESQL_RED_URL
At this point, your database should be up and running. Now, let's edit your Laravel config to point to the PostgreSQL database.
Once again, if this is a real app, you're going to want to only be making these changes in your production configuration settings, but for now, we're just hacking at a dummy app.
Then, just like we did with MySQL, set the following at the top of your database.php:
Then, Change the default value to pgsql
If you want to add PostgreSQL from Heroku, you can also add from https://data.heroku.com.
herokuaddons:create heroku-postgresql:hobby-dev
The environment variable for your PostgreSQL database URL has a COLOR variable in the name itself:
- HEROKU_POSTGRESQL_PINK_URL,
- HEROKU_POSTGRESQL_BLUE_URL, etc.
Depending on the server you're on, that color may be different.
That means you can't necessarily rely on the name of that environment variable always being the same.
So, you want to be sure to not rely on the HEROKU_POSTGRESQL_COLOR_URL for your database configurations.
Read on for how to handle it instead.
At any point, you can find both the name of your PostgreSQL variable and its value by running the following:
heroku config | grep HEROKU_POSTGRESQL
HEROKU_POSTGRESQL_RED_URL: postgres://user3123:passkja83kd8@ec2-117-21-174-214.compute-1.amazonaws.com:6212/db982398
If you check out your heroku config, you should now see that you have a DATABASE_URL that's set to the same value as the HEROKU_POSTGRESQL_COLOR_URL.
At this point, your database should be up and running. Now, let's edit your Laravel config to point to the PostgreSQL database.
Configuring Laravel to use PostgreSQL
Once again, if this is a real app, you're going to want to only be making these changes in your production configuration settings, but for now, we're just hacking at a dummy app.
Then, just like we did with MySQL, set the following at the top of your database.php:
url = parse_url(getenv("DATABASE_URL"));
'default' => 'pgsql',
Then change your pgsql entry in that same file to be the following:
'pgsql' => array( 'driver' => 'pgsql','host' => $url["host"],
'database' => substr($url["path"], 1),
'username' => $url["user"],
'password' => $url["pass"],
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public'
),
That's it! Commit and push and migrate:
Congratulations! Now, you Install Laravel on Heroku - Using a PostgreSQL database.
git add .
git commit -m "Convert to use Heroku PostgreSQL database"
git push heroku master
heroku run php artisan migrate
Check out your Heroku URL in the browser, and you should see the app running just like it was when it was MySQL: []
3 Comments
Really cool article man. Let me know if you are going to make next articles about PostgreSQL. I mean CRUD operation in Laravel + Heroku + PostgreSQL.
ReplyDeleteThanks. Please follow my blogs to read articles.
DeleteWaiting for crud operation
ReplyDelete