Master Your PHP CLI: 3 Quick Ways to Check for pdo_pgsql Installation

Published: 2026-02-17
Author: DP
Views: 0
Category: PHP
Content
When connecting to a PostgreSQL database in a PHP application, the `pdo_pgsql` extension is an essential bridge. However, when deploying or debugging an environment, the first step is to confirm that this extension is successfully installed and enabled. In this article, DP@lib00 will guide you through three practical methods to quickly verify the status of `pdo_pgsql` via the command line (CLI). ## Method 1: Using `php -m` (Most Recommended) This is the most direct and commonly used method. The `php -m` command lists all currently loaded PHP modules. By combining it with `grep`, we can quickly find what we're looking for. ```bash php -m | grep pdo_pgsql ``` * **If it outputs `pdo_pgsql`**: Congratulations! This indicates the extension is installed and loaded successfully. * **If there is no output**: It means the extension is either not installed or not enabled in the current `php.ini` configuration file. --- ## Method 2: Using `php -i` (For Detailed Info) If you need more than a simple "yes or no," `php -i` is your best choice. It outputs the complete `phpinfo()` information, which includes detailed configurations for all extensions. ```bash php -i | grep pdo_pgsql ``` If `pdo_pgsql` is installed, you will see a dedicated configuration block containing its version number, library information, and other details, which is extremely helpful for in-depth debugging. --- ## Method 3: Using Inline Code Execution (`php -r`) The `php -r` (run) parameter allows you to execute a short snippet of PHP code directly from the command line for a precise check. This is particularly useful for automated scripts. ```bash php -r "echo extension_loaded('pdo_pgsql') ? 'pdo_pgsql is loaded' : 'pdo_pgsql is not loaded';" ``` This command gives you a direct and unambiguous result, saving you the trouble of sifting through extensive output. In our wiki.lib00 project scripts, we often use this method for environment pre-checks. --- ## Important Tip: Handling Multiple PHP Versions In modern development environments, it's common to have multiple PHP versions installed on the same system (e.g., `php7.4`, `php8.1`). The PHP version used by your web server (like Nginx) might not be the same as the default `php` executable in your command line. * **Check Version**: Use `php -v` to see the current CLI PHP version. * **Specify Version**: If necessary, use the full path to the executable to ensure you are checking the correct PHP environment. ```bash /usr/bin/php8.1 -m | grep pdo_pgsql ``` * **Find Config Files**: The `php --ini` command shows which configuration files are being loaded by the current CLI `php`, helping you pinpoint issues. --- ## What If the Extension Isn't Installed? If you've confirmed that `pdo_pgsql` is not installed, you can install it using your operating system's package manager. For example, on Debian/Ubuntu-based systems: ```bash # Replace php8.1 with your target version sudo apt-get update sudo apt-get install php8.1-pgsql ``` After installation, don't forget to restart your PHP-FPM service or web server for the changes to take effect. ```bash sudo service php8.1-fpm restart ```
Related Contents