Slim4: could not find driver

I’m new to slim.
Created a new slim app with composer create-project slim/slim-skeleton:dev-master helloSlim
I’m using docker-compose to run the development server and my database is hosted in heroku
I’m trying to setup postgres database with slim and I keep getting this error. Can’t figure out the error.
I followed the blog post.

    "statusCode": 500,
    "error": {
        "type": "SERVER_ERROR",
        "description": "could not find driver"

Here’s what my settings.php looks like



use App\Application\Settings\Settings;
use App\Application\Settings\SettingsInterface;
use DI\ContainerBuilder;
use Monolog\Logger;

return function (ContainerBuilder $containerBuilder) {

    // Global Settings Object
        SettingsInterface::class => function () {
            return new Settings([
                'displayErrorDetails' => true, // Should be set to false in production
                'logError'            => false,
                'logErrorDetails'     => false,
                'logger' => [
                    'name' => 'slim-app',
                    'path' => isset($_ENV['docker']) ? 'php://stdout' : __DIR__ . '/../logs/app.log',
                    'level' => Logger::DEBUG,
                'db' => [
                    'driver' => $_ENV['DB_DRIVER'],
                    'host' =>$_ENV['DB_HOST'],
                    'port' =>$_ENV['DB_PORT'],
                    'database' =>$_ENV['DB_NAME'],
                    'username' =>$_ENV['DB_USERNAME'] ,
                    'password' =>$_ENV['DB_PASSWORD'],

I adedd the PDO::class into the dependency


// ...

return function (ContainerBuilder $containerBuilder) {
       // ...
        PDO::class => function (ContainerInterface $c) {
            $settings = $c->get(SettingsInterface::class);
            $dbSettings = $settings->get('db');

            $host = $dbSettings['host'];
            $dbname = $dbSettings['database'];
            $username = $dbSettings['username'];
            $password = $dbSettings['password'];
            $port = $dbSettings['port'];
            $driver = $dbSettings['driver'];
            $dsn = "$driver:host=$host;port=$port;dbname=$dbname";
            return new PDO($dsn, $username, $password);

Make sure that you have installed / compiled the PDO PostgreSQL extension for PHP.

Already have that installed

I have heard that PDO and postgres is quite difficult to set up.

What is the result of this?


Have you tried using pg_connect function instead of PDO? (I guess it’s also faster).

Simple check:

$connection = pg_connect('host=localhost port=5432 user=username password=foo dbname=mydatabase connect_timeout=5');
$result = pg_query($connection, "select * from pg_stat_activity");

Looks like I had not enabled pdo for pgsql
uncommenting these lines in /etc/php/7.4/apache2/php.ini helped


I’m new to slim and still don’t know what are the best practises.
Should I use eloquent or stick to pdo?

The Slim 4 tutorial uses PDO for learning purposes only, because PDO is already installed. In a real application you better use a QueryBuilder to create the SQL statements. I’m not a big fan of ORM’s in general because I prefer SOLID, so I do not recommend any ORM.

In my blog you can find more information about this topics:

1 Like