PostgreSql 'PDOException' with message 'could not find driver'

PostgreSql 'PDOException' with message 'could not find driver'

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Resolving PDOException: could not find driver with PostgreSQL

When your MySQL queries execute flawlessly but switching to PostgreSQL triggers a PDOException with the message "could not find driver," it usually points to a specific extension mismatch rather than a syntax error in your connection string. You might have installed the php5-pgsql package on your Debian or Ubuntu system, which correctly provides the standard PostgreSQL adapter and includes pdo_pgsql. However, PHP does not automatically load every available extension simply because the package is installed; the driver must be explicitly enabled or detected by the SAPI (Server API) in use.

The error typically manifests when using a DSN like pgsql:dbname=DB;host=192.168.0.2 within an application framework like Laravel or a custom script. While the underlying C extension exists, the PHP runtime needs to know that it should load it. In many shared hosting environments or containerized setups, this requires adding specific lines to php.ini. If you are running CLI tests, ensure that the php.ini file being used by the command line matches the one configured for your web server (Apache/Nginx).

A common pitfall occurs when developers assume that installing a package like php5-pgsql on an older distribution, such as Ubuntu 10.04, is sufficient without verifying the active configuration. On legacy systems, you might need to restart the web server after editing php.ini, or in some cases, the extension might be compiled but disabled by default (;extension=pgsql.so). Verify this by running php -m from the terminal. If pdo_pgsql is missing from the list despite the package being present, you may need to uncomment the line corresponding to the driver in the configuration file or ensure the correct php.ini is loaded for your specific PHP version (e.g., PHP 5.x vs modern PHP 8).

If you are using a framework like Laravel, this error often appears during the database migration process or when attempting to connect to the queue. The framework attempts to instantiate the PDO driver upon initialization. If the extension is not loaded, the instantiation fails immediately with the "could not find driver" exception before any SQL logic runs. To resolve this programmatically without restarting the server (though a restart is recommended for production), you can add extension=pdo_pgsql to your configuration file or ensure that the SAPI loads all extensions from the conf.d directory where the package files are typically stored.

It is also worth noting that on some systems, the order of PHP versions matters. If you have multiple PHP installations (e.g., one for CLI and one for Apache), installing packages might affect only one of them. Check which PHP binary your application is invoking using which php or phpenv. If the error persists after enabling the extension in php.ini, inspect the error log of your web server for warnings regarding missing shared objects, which can indicate that the .so file was not found at the expected path due to library dependencies or incorrect architecture (32-bit vs 64-bit).

Finally, ensure your DSN is correctly formatted. While pgsql:dbname=DB;host=192.168.0.2 is valid, sometimes specifying the port explicitly avoids ambiguity if the default 5432 is not being used. Once the driver is confirmed loaded via php -m and the configuration is aligned, the connection should establish successfully, allowing your application to interact with PostgreSQL seamlessly.

Our network