Laravel on Heroku - Using a PostgreSQL database
Posted on May 2, 2014
!Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.
In the last two posts we got Laravel up and running on Heroku, and then connected it with MySQL. But Heroku natively prefers PostgreSQL, and you'll find that PostgreSQL can do everything MySQL can do, and a lot more. So let's get it running on our Laravel Heroku app.
Adding PostgreSQL to your Heroku app
First, navigate to your app directory and add PostgreSQL as a Heroku add-on (if you haven't followed the first tutorial, you'll need to do that first to install the Heroku toolset, get this Laravel app connected to a Heroku app, etc.):
$ heroku addons:create heroku-postgresql:hobby-dev
You should see output like this:
Adding heroku-postgresql:hobby-dev on app-name-here... done, v14 (free) Attached as HEROKU_POSTGRESQL_COLOR_URL Database has been created and is available ! This database is empty. If upgrading, you can transfer ! data from another database with pgbackups:restore. Use `heroku addons:docs heroku-postgresql` to view documentation.
The environment variable for your PostgreSQL database URL has a
COLOR variable in the name itself:
HEROKU_POSTGRESQL_BLUE_URL, etc... and 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
You should see something like the following:
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. That is the environment variable you want to work from.
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.
Configuring Laravel to use PostgreSQL
Once again, if this is 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.
First, change the value of
'default' => 'pgsql',
Then, just like we did with MySQL, set the following at the top of your database.php:
$url = parse_url(getenv("DATABASE_URL")); $host = $url["host"]; $username = $url["user"]; $password = $url["pass"]; $database = substr($url["path"], 1);
Then change your
pgsql entry in that same file to be the following:
'pgsql' => array( 'driver' => 'pgsql', 'host' => $host, 'database' => $database, 'username' => $username, 'password' => $password, 'charset' => 'utf8', 'prefix' => '', 'schema' => 'public', ),
That's it! Commit and push and migrate:
$ git add . $ git commit -m "Convert to use Heroku PostgreSQL database" $ git push heroku master $ heroku run php /app/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:
Congratulations! You're now a Laravel + Heroku + database pro.
- Heroku has a page dedicated to helping you migrate your data from MySQL to PostgreSQL
Comments? I'm @stauffermatt on Twitter
Tags: php • heroku • laravel • postgresql
This is part of a series of posts on Laravel on Heroku:
Apr 30, 2014 | php, laravel, heroku
May 1, 2014 | php, laravel, mysql, heroku
May 2, 2014 | php, heroku, laravel, postgresql
Jun 29, 2014 | laravel, heroku, nginx