Laravel on Heroku - Using a PostgreSQL database

Posted on May 02, 2014 | By Matt Stauffer

(This is part of a series of posts on Laravel on Heroku. Check back soon for more.)

  1. Installing a Laravel app on Heroku
  2. Laravel on Heroku - Using a MySQL database
  3. Laravel on Heroku - Using a PostgreSQL database
  4. Laravel on Heroku - Using a Buildpack locally to mimic your Heroku environment (Nginx)
  5. Laravel on Heroku - Using a Buildpack locally to mimic your Heroku environment (Apache) (coming soon)

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:add heroku-postgresql:dev

You should see output like this:

Adding heroku-postgresql: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_PINK_URL, 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:

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. 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' in app/config/database.php to 'pgsql'.

    '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.

Notes #


Tags: php | heroku | laravel | postgresql

Matt Stauffer headshot

Hi, I'm Matt Stauffer.

I'm a designer, developer, and communicator.

I'm partner & technical director at Tighten Co.

You can find me on Twitter at @stauffermatt