PHP PDO连接PostgreSQL报错:Could not find Driver 求助
Hey there, let's work through this PDO driver issue together—new tech can feel overwhelming when things don't click right away, so let's break down what might be missing after the steps you've already tried.
First, let's recap what you've done to make sure we're on the same page:
- Confirmed Apache is using your Homebrew-installed PHP (output from
which phpis/usr/local/bin/php) - Uncommented the PDO PostgreSQL extension line in your
php.ini
Now let's dig into the common gaps that might still be causing the error:
1. Verify you're editing the correct php.ini file
It's super easy to edit the wrong php.ini when using Homebrew PHP. Run this command to find exactly which config file your PHP is loading:
php --ini
Look for the line that says Loaded Configuration File—that's the one you need to modify. If you edited a different file earlier, the changes won't take effect.
2. Double-check the extension name in php.ini
Homebrew's PHP builds often use a simpler extension name for PDO PostgreSQL. Instead of extension=php_pdo_pg..., try changing it to:
extension=pdo_pgsql
Some older versions might still use the prefixed name, but the shorter form is standard for modern Homebrew PHP installs.
3. Make sure PostgreSQL is installed on your system
The PDO driver needs PostgreSQL to exist on your machine to work. Check if it's installed with:
psql --version
If you get a "command not found" error, install it via Homebrew:
brew install postgresql
4. Restart Apache after making config changes
This is a common oversight! Any edits to php.ini require restarting your Apache server to take effect. Run:
sudo apachectl restart
5. Confirm the extension is actually loaded
After restarting, check if the PDO PostgreSQL extension is active by running:
php -m | grep pdo
You should see pdo_pgsql in the output. If you don't, try reinstalling your Homebrew PHP to ensure the extension is included:
brew reinstall php
For specific PHP versions (like php@8.1), use:
brew reinstall php@8.1
6. Ensure Apache is using the correct PHP module
Even if which php points to Homebrew's PHP, Apache might still be loading the system's default PHP module. Open your Apache config file (usually /usr/local/etc/httpd/httpd.conf or /etc/httpd/httpd.conf) and verify the LoadModule line for PHP points to your Homebrew installation. For example:
LoadModule php_module /usr/local/opt/php/lib/httpd/modules/libphp.so
Don't forget to restart Apache after updating this line too!
Give these steps a try, and let me know if any of them resolve the error. If not, we can dig deeper into version-specific quirks.
内容的提问来源于stack exchange,提问作者Nate

