1

I have been working on a project that requires I set it up in Heroku. The Database I have been using thus far locally is mySQL, but I am looking to use Postgre when the project is on Heroku.

I have done a large amount of searching but have yet to find an answer as to how I configure my Laravel project to use Postgres and how do I perform basic functions such as adding a new database to Postgres.

If there is an alternative way to just use mySQL as is that would be great.

Thanks for the help.

1

1 Answer 1

2

Heroku places (once you provision a Heroku Postgres instance) the database credentials in the DATABASE_URL environment variable, in the following format:

postgres://username:password@hostname:port/database

Now, you could manually fill out your DB_HOST, DB_USERNAME, etc. .env vars from this, but there's a better way: you can parse the URL in your config/database.php file.

Note: Newer versions of Laravel now support a DATABASE_URL .env value directly. No need to parse anymore.

'pgsql' => [
    'driver' => 'pgsql',
    'host' => parse_url(env('DATABASE_URL'), PHP_URL_HOST),
    'port' => parse_url(env('DATABASE_URL'), PHP_URL_PORT),
    'database' => ltrim(parse_url(env('DATABASE_URL'), PHP_URL_PATH), '/'),
    'username' => parse_url(env('DATABASE_URL'), PHP_URL_USER),
    'password' => parse_url(env('DATABASE_URL'), PHP_URL_PASS),
    'charset' => 'utf8',
    'prefix' => '',
    'schema' => 'public',
    'sslmode' => 'prefer',
],

(Make sure you set DB_CONNECTION to pgsql so this connection is used!)

how do I perform basic functions such as adding a new database to Postgres

You don't. A Heroku Postgres instance comes with one database.

Sign up to request clarification or add additional context in comments.

5 Comments

Side note: If you're going to use Postgres, you should be doing so locally too - there can be differences between the two, so you may find that some migrations don't work etc. Otherwise, there are third-party Heroku providers for MySQL databases. elements.heroku.com/addons/jawsdb elements.heroku.com/addons/jawsdb-maria or you could just spin up something like an AWS RDS instance and point your config at it.
Great, thank you for the assistance, I do have one more question, there is already a pgsql entry in the database.php, should I replace it with the connection you gave me above?
@TimothyHayes That's up to you. You could create a new connection titled something like 'heroku' instead of 'pgsql' and set DB_CONNECTION to that. Personally, I just use the one and set my local .env to have a DATABASE_URL setting that has my localhost settings in it.
Cheers for all the help mate
Thanks, this works, here's how my .env looks like in case someone needs for reference: DATABASE_URL=postgres://homestead:[email protected]:5432/getbookmark

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.