How to configure a Laravel application to use heroku PostgreSQL hobby dev?

╄→尐↘猪︶ㄣ 提交于 2020-01-25 21:41:27

问题


I am currently working on a Laravel project. The default database connection points to the mySQL driver. How to change the driver to Postgres and use the credentials provided by heroku?


回答1:


First, add the below code on top of the database configuration file.

$host = env('DB_HOST', '127.0.0.1');
$database = env('DB_DATABASE', '');
$username = env('DB_USERNAME', 'forge');
$password = env('DB_PASSWORD', 'forge');


if($databaseUrl = getenv('DATABASE_URL')) {

    $url = parse_url($databaseUrl);

    $host = $url['host'];
    $username = $url['user'];
    $password = $url['pass'];
    $database = substr($url['path'], 1);
}

Now in the same file change the value of the pgSQL key like this,

'pgsql' => [
            'driver' => 'pgsql',
            'host' => $host,
            'port' => env('DB_PORT', '5432'),
            'database' => $database,
            'username' => $username,
            'password' => $password,
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'prefer',
        ]

Now login to your heroku account, under the settings tab of your project add two configuration variables,

  1. Key: DATABASE_URL and value: the connection string provided by heroku while creating the hobby dev instance
  2. Key: DB_CONNECTION and value: pgsql


来源:https://stackoverflow.com/questions/45488070/how-to-configure-a-laravel-application-to-use-heroku-postgresql-hobby-dev

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!