How do I enable php to work with postgresql?
231,931
Solution 1
Try this:
Uncomment the following in php.ini by removing the ";"
;extension=php_pgsql.dll
Use the following code to connect to a postgresql database server:
pg_connect("host=localhost dbname=dbname user=username password=password")
or die("Can't connect to database".pg_last_error());
Solution 2
You need to install the pgsql module for php. In debian/ubuntu is something like this:
sudo apt-get install php5-pgsql
Or if the package is installed, you need to enable de module in php.ini
extension=php_pgsql.dll (windows)
extension=php_pgsql.so (linux)
Greatings.
Solution 3
For debian/ubuntu install
sudo apt-get install php-pgsql
Solution 4
- SO: Windows/Linux
- HTTP Web Server: Apache
- Programming language: PHP
Enable PHP to work with PostgreSQL in Apache
In Apache I edit the following configuration file: C:\xampp\php.ini
I make sure to have the following lines uncommented:
extension=php_pgsql.dll
extension=php_pdo_pgsql.dll
Finally restart Apache before attempting a new connection to the database engine.
Also, I leave my code that ensures that the connection is unique:
private static $pdo = null;
public static function provideDataBaseInstance() {
if (self::$pdo == null) {
$dsn = "pgsql:host=" . HOST .
";port=5432;dbname=" . DATABASE .
";user=" . POSTGRESQL_USER .
";password=" . POSTGRESQL_PASSWORD;
try {
self::$pdo = new PDO($dsn);
} catch (PDOException $exception) {
$msg = $exception->getMessage();
echo $msg .
". Do not forget to enable in the web server the database
manager for php and in the database instance authorize the
ip of the server instance if they not in the same
instance.";
}
}
return self::$pdo;
}
GL
Solution 5
I have to add in httpd.conf this line (Windows):
LoadFile "C:/Program Files (x86)/PostgreSQL/8.3/bin/libpq.dll"
Author by
Aaron
Updated on September 25, 2021Comments
-
Aaron over 2 years
<?php try { $dbh = new PDO('pgsql:host=localhost;port=5432;dbname=###;user=###;password=##'); echo "PDO connection object created"; } catch(PDOException $e) { echo $e->getMessage(); } ?>
I get the error message "Could Not Load Driver"
-
Aaron about 12 yearsHi do you know how I would install it in windows? Sorry I don't have ubuntu yet..
-
Aaron about 12 yearsHow do I install that package on windows?
-
rinat.io about 12 yearsMaybe
pecl install pdo_pgsql
. Not sure -
Jorge Olivares about 12 yearsDepends. You are using wamp or something like that?
-
Jorge Olivares about 12 yearstry to enable the module first, editing php.ini and searching for php_pgsql and remove the ; in the begining of the line. Then you need to reload your service.
-
Maks3w almost 10 yearsAnd reload the PHP config (reload apache/nginx etc) after install new extensions
-
IIllIIll about 9 yearsI'm still seeing
PHP Fatal error: Call to undefined function pg_connect()
, although I know I've uncommented it. Ideas? -
wmfrancia about 7 yearsJust an update on this old thread for php7 the command is: sudo apt install php7.0-pgsql
-
Matthew Lock over 6 yearsI do installed PHP via WPI, and it was enough for me to add extension=php_pgsql.dll to C:\Program Files\PHP\v7.1\php.ini at the end of the [ExtensionList] section
-
Daniel L. VanDenBosch about 6 yearsWhere would one find httpd.conf
-
Daniel L. VanDenBosch about 6 yearsAwesome thanks, if I were using xammp would I put this in the C:\xampp folder?
-
Floggedhorse about 5 yearsRunning php 7.0 - I had to add "extension=pgsql.so" to "php.ini" = "open database successfully" = :)
-
Rishu Ranjan almost 3 yearsinside config file for your apache server. If you are using mamp, then C:\MAMP\bin\apache\conf\httpd.conf
-
Eduardo Sebastian over 2 yearsGenerally, the command is sudo apt-get install phpVersion-pgsql