如何配置Laravel应用程序以使用heroku PostgreSQL爱好开发人员? [英] How to configure a Laravel application to use heroku PostgreSQL hobby dev?

查看:29
本文介绍了如何配置Laravel应用程序以使用heroku PostgreSQL爱好开发人员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究Laravel项目.默认的数据库连接指向mySQL驱动程序.如何将驱动程序更改为Postgres并使用heroku提供的凭据?

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?

推荐答案

首先,将以下代码添加到数据库配置文件的顶部.

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);
}

现在在同一文件中,像这样更改pgSQL键的值,

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',
        ]

现在登录到您的heroku帐户,在项目的设置"标签下添加两个配置变量,

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

  1. 键:DATABASE_URL和值:heroku在创建业余dev实例时提供的连接字符串
  2. 键:DB_CONNECTION和值:pgsql

这篇关于如何配置Laravel应用程序以使用heroku PostgreSQL爱好开发人员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆