问题
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,
- Key: DATABASE_URL and value: the connection string provided by heroku while creating the hobby dev instance
- Key: DB_CONNECTION and value: pgsql
来源:https://stackoverflow.com/questions/45488070/how-to-configure-a-laravel-application-to-use-heroku-postgresql-hobby-dev