本地Google App Engine环境下PHP+CodeIgniter连接PostgreSQL报错:pg_connect未定义
Hey there! That error you're seeing—Call to undefined function pg_connect()—happens because your PHP installation doesn't have the PostgreSQL extension enabled. Let's get this sorted out quickly, since you're using XAMPP (I spotted the /opt/lampp/ path in your error log):
Step 1: Locate your php.ini file
On Linux, XAMPP stores the main php.ini file at:
/opt/lampp/etc/php.ini
Open this file with a text editor (like nano or vim) using sudo to make changes:
sudo nano /opt/lampp/etc/php.ini
Step 2: Enable the PostgreSQL extensions
Search for these two lines in the php.ini file:
;extension=pgsql ;extension=pdo_pgsql
Remove the leading semicolon (;) from both lines to uncomment them. They should look like this after editing:
extension=pgsql extension=pdo_pgsql
This enables both the core PostgreSQL extension and the PDO driver, which CodeIgniter might use depending on your setup.
Step 3: Restart Apache
For the changes to take effect, restart XAMPP's Apache server. You can do this via the command line:
sudo /opt/lampp/lampp restart apache
Or if you prefer using the XAMPP control panel, just stop and start the Apache service there.
Step 4: Verify the extension is enabled
Create a simple PHP file (let's call it phpinfo.php) in your htdocs directory with this content:
<?php phpinfo(); ?>
Access it via your browser (e.g., http://localhost/phpinfo.php) and search for "pgsql". You should see a section with details about the PostgreSQL extension—this confirms it's active.
Bonus: Double-check CodeIgniter Database Config
Make sure your CodeIgniter database config file (application/config/database.php) is set up correctly for PostgreSQL:
$db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', // or your PostgreSQL host 'username' => 'your_db_username', 'password' => 'your_db_password', 'database' => 'your_db_name', 'dbdriver' => 'postgre', // This is crucial—must be 'postgre' 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE );
Ensure the dbdriver is set to postgre—a common mistake is using pgsql here, which won't work with CodeIgniter's postgre driver.
If you've followed all these steps and still run into issues, double-check that your XAMPP installation includes the PostgreSQL extension files. Some minimal XAMPP builds might omit them, so you may need to reinstall XAMPP with full components or manually add the extension files to your ext directory (though that's less common these days).
内容的提问来源于stack exchange,提问作者Kiran Suthar

