Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

klik's avatar
Level 1

Lumen connection with postgresql on heroku

Hi. I have a problem with connection to DB on heroku.

So far I have done the following steps:

  1. Created fresh Lumen project.
  2. Deployed project on heroku using this guide - https://devcenter.heroku.com/articles/getting-started-with-laravel
  3. Created fresh DB instance using this guide - https://mattstauffer.com/blog/laravel-on-heroku-using-a-postgresql-database/
  4. Installed plugin to have access to artisan commands - https://github.com/flipboxstudio/lumen-generator
  5. Created database configuration file to get access to postgresql db in heroku.
  6. Configured logging sysystem.

Finally on my localhost xampp everything works prefect. But the problem is on heroku.

When I try to do "php artisan migrate" then I have following error: " In Connection.php line 664:

SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from inform ation_schema.tables where table_schema = forge and table_name = migrations)

In Connector.php line 70:

SQLSTATE[HY000] [2002] No such file or directory"

But this above error appears only when I remove logging instruction from "bootstrap.php/app.php" file. When I add instructions then I have this error: "In app.php line 33:

Call to undefined method Laravel\Lumen\Application::configureMonologUsing()"

So I added some tables to DB manually but when I try retrieve data from DB then in my browser see error code 500 and this information. "Whoops, looks like something went wrong." And my log in heroku shows me only this information:

2018-10-05T14:57:26.092216+00:00 heroku[router]: at=info method=GET path="/" host=still-harbor-30058.herokuapp.com request_id=68d0c450-70b8-4259-aa01-9b6ea3ac672f fwd="2.121.94.235" dyno=web.1 connect=0ms service=116ms status=500 bytes=1845 protocol=https

2018-10-05T14:57:26.089888+00:00 app[web.1]: 10.95.199.248 - - [05/Oct/2018:14:57:25 +0000] "GET / HTTP/1.1" 500 1621 "-" "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0

And again on my localhost everything works perfect.

Please advice me how should write correct lumen configuration to work on heroku.

Before I try to deploy Lumen, I installed Laravel on heroku and then everything was fine.

Code bellow:

.env

APP_ENV=local
APP_DEBUG=true
APP_KEY=base64:hixVqQUz1Fw2MgQZK0pDJDmEhn9nvP52JWvYQlR2sEE=
APP_TIMEZONE=UTC

LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=

CACHE_DRIVER=file
QUEUE_CONNECTION=sync

"bootstrap/app.php"

<?php

require_once __DIR__.'/../vendor/autoload.php';

try {
    (new Dotenv\Dotenv(dirname(__DIR__)))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
    //
}


$app = new Laravel\Lumen\Application(
    dirname(__DIR__)
);
$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);
$app->register(Appzcoder\LumenRoutesList\RoutesCommandServiceProvider::class);
$app->withFacades();
$app->configure('database');
$app->withEloquent();
$app->configureMonologUsing(function($monolog) {
    $monolog->pushHandler(new \Monolog\Handler\StreamHandler('php://stderr', \Monolog\Logger::WARNING));
    return $monolog;
});

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

// $app->middleware([
//     App\Http\Middleware\ExampleMiddleware::class
// ]);

// $app->routeMiddleware([
//     'auth' => App\Http\Middleware\Authenticate::class,
// ]);

// $app->register(App\Providers\AppServiceProvider::class);
// $app->register(App\Providers\AuthServiceProvider::class);
// $app->register(App\Providers\EventServiceProvider::class);

$app->router->group([
    'namespace' => 'App\Http\Controllers',
], function ($router) {
    require __DIR__.'/../routes/web.php';
});

return $app;

"config/database.php"

<?php
$url = parse_url(getenv("DATABASE_URL"));

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

   

    'fetch' => PDO::FETCH_CLASS,

    'default' => env('DB_CONNECTION', 'pgsql'),

 
    'connections' => [

        'testing' => [
            'driver' => 'sqlite',
            'database' => ':memory:',
        ],

        'sqlite' => [
            'driver'   => 'sqlite',
            'database' => env('DB_DATABASE', base_path('database/database.sqlite')),
            'prefix'   => env('DB_PREFIX', ''),
        ],

        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', '127.0.0.1'),
            'port'      => env('DB_PORT', 3306),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => env('DB_CHARSET', 'utf8mb4'),
            'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'),
            'prefix'    => env('DB_PREFIX', ''),
            'timezone'  => env('DB_TIMEZONE', '+00:00'),
            'strict'    => env('DB_STRICT_MODE', false),
        ],

        'pgsql' => [
            'driver'   => 'pgsql',
            'host'     => $host,
            'database' => $database,
            'username' => $username,
            'password' => $password,
            'charset'  => 'utf8',
            'prefix'   => '',
            'schema'   => 'public',
        ],

        'sqlsrv' => [
            'driver'   => 'sqlsrv',
            'host'     => env('DB_HOST', 'localhost'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset'  => env('DB_CHARSET', 'utf8'),
            'prefix'   => env('DB_PREFIX', ''),
        ],

    ],


    'migrations' => 'migrations',
 
    'redis' => [

        'client' => 'predis',

        'cluster' => env('REDIS_CLUSTER', false),

        'default' => [
            'host'     => env('REDIS_HOST', '127.0.0.1'),
            'port'     => env('REDIS_PORT', 6379),
            'database' => env('REDIS_DATABASE', 0),
            'password' => env('REDIS_PASSWORD', null),
        ],

    ],

];
0 likes
7 replies
jaythanki's avatar

@klik you should check first your heroku postgres info by below command in your heroku cli

heroku pg:info

It will show you

=== DATABASE_URL
Plan:                  Hobby-dev
Status:                Available
Connections:           0/20
PG Version:            10.4
Created:               2018-09-20 16:40 UTC
Data Size:             7.9 MB
Tables:                3
Rows:                  7/10000 (In compliance)
Fork/Follow:           Unsupported
Rollback:              Unsupported
Continuous Protection: Off
Add-on:                postgresql-octagonal-21762
klik's avatar
Level 1

I did it and I had correct informations.

jaythanki's avatar

@klik try this

composer dump-autoload

than try to run again migrate command

php artisan migrate

or if error occur in migrate than run

php artisan migrate:refresh
//OR
php artisan migrate:fresh
klik's avatar
Level 1

Since I wrote that subject, I installed Lumen 3 times on heroku using always fresh installation. Finally I connected to DB. I can migrate and retrieve data from DB.

Thank you for help.

But one problem left. When I add this logging instructions -

    $monolog->pushHandler(new \Monolog\Handler\StreamHandler('php://stderr', \Monolog\Logger::WARNING));
    return $monolog;
});

to app.php then I have error:

In app.php line 29: Call to undefined method Laravel\Lumen\Application::configureMonologUsing()

Any advice please?

jaythanki's avatar

@klik so far i found one solution

change .env file

LOG_CHANNEL=daily

but didn't try yet

Please or to participate in this conversation.