44

I am trying to connect to my database (remote server) which has PostgreSQL installed in it. My PHP code is trying to connect to the database using pg_connect(), but I get the error saying:- "Fatal error: Call to undefined function pg_connect() in /var/www/website/functions.php on line 82".

The line 82 simply is:

$db = pg_connect($conn_string);
where $conn_string = "host=".$hostname." port=5432 dbname=".$dbname." user=".$db_user." password=".$db_password.""

(all variables defined earlier)

I checked many forums and the only solution suggested was locating the php.ini file which contains a line:- extension = pgsql.so (for UNIX) and extension = php_pgsql.dll (for Windows).

This statement is supposed to be commented and the solution is to uncomment it. I have tried it but still does not change the situation. The remote server has a version later than PostgreSQL v9.0.4 installed. I then installed PostgreSQL v8.4.8 on to my laptop and ran the website locally using MAMP. At first, Apache crashed for some odd reason, I fixed that problem but again I ended up with the same error as before i.e. Fatal error: Call to undefined function pg_connect()....

I also ran the phpinfo() and it showed that the php version does support the PostgreSQL module.I have spent an entire day searching for the solution but have been unsuccessful. This is my first project developing a website and I am out of wits. Any kinda help will be highly appreciated.

phpinfo() gives me a huge list of things at the terminal but the listings relevant to PostgreSQL are as follows:-

pdo_pgsql

PDO Driver for PostgreSQL => enabled
PostgreSQL(libpq) Version => 9.0.4
Module version => 1.0.2
Revision =>  $Id: pdo_pgsql.c 306939 2011-01-01 02:19:59Z felipe $ 

pgsql

PostgreSQL Support => enabled
PostgreSQL(libpq) Version => 9.0.4
Multibyte character support => enabled
SSL support => enabled
Active Persistent Links => 0
Active Links => 0

Directive => Local Value => Master Value
pgsql.allow_persistent => On => On
pgsql.auto_reset_persistent => Off => Off
pgsql.ignore_notice => Off => Off
pgsql.log_notice => Off => Off
pgsql.max_links => Unlimited => Unlimited
pgsql.max_persistent => Unlimited => Unlimited

I had restarted MAMP after every edit I made since it was mentioned in every post I have read so far. I believe that resets both Apache and php.

'pqsql.so' (which is the UNIX equivalent of 'php_pqsql.dll' in Windows) is present in the 'extension' directory. I also copy-pasted the 'pqsql.so' file on to the Apache/bin directory but it did not give me any change.

I am not running php in the command line primarily. I just was curious to see what phpinfo() would give me relevant to pgsql which I have mentioned in my reply above.

I am still working on the tools you have mentioned and will respond as soon as I get any results.

Thanks, H

hrshd
  • 941
  • 2
  • 13
  • 19
  • 1
    What is `phpinfo()` showing exactly in regards to PostreSQL? Have you restarted the web server in between? – Pekka Sep 15 '11 at 22:18
  • 1
    +1 for running phpinfo(). It will undoubtedly show that the pgsql extension is not loaded. You may have to specify the full path to the extension in your php.ini, and restart apache. – Brendan Sep 09 '12 at 08:47

17 Answers17

63

You need to install the php-pgsql package or whatever it's called for your platform. Which I don't think you said by the way.

On Ubuntu and Debian:

sudo apt-get install php-pgsql
p_mcp
  • 2,643
  • 8
  • 36
  • 75
Scott Marlowe
  • 8,490
  • 3
  • 23
  • 21
21

Easy install for ubuntu:

Just run:

sudo apt-get install php5-pgsql

then

sudo service apache2 restart //restart apache

or

Uncomment the following in php.ini by removing the ;

;extension=php_pgsql.dll

then restart apache

Dharman
  • 30,962
  • 25
  • 85
  • 135
Angelin Nadar
  • 8,944
  • 10
  • 43
  • 53
12

Fatal error: Call to undefined function pg_connect()...

I had this error when I was installing Lampp or xampp in Archlinux,

The solution was edit the php.ini, it is located in /opt/lampp/etc/php.ini

then find this line and uncomment

extension="pgsql.so"

then restart the server apache with xampp and test...

Dharman
  • 30,962
  • 25
  • 85
  • 135
davidleosam
  • 344
  • 3
  • 4
6

Edit. I just noticed you were mentionning MAMP. My advice is for Windows but may be useful if you know what corresponding tools to use.

Things to try:

  • Have you restarted PHP and Apache since your editing of php.ini?

  • Is the php_pgsql.dll found in your php\ext directory?

  • Are you running php as a module? If so, try copying the php_pgsql.dll file in the Apache\bin directory.

  • Are you running PHP from the command line with a flag specifying a different php.ini file?

  • You could try using a tool such as Sysinternals' Filemon to view what files are attempting to be accessed when running PHP.

  • You could try using a tool such as Dependency Walker to look at the dependencies for the postgreSQL DLL, in case you have a missing dependency. Quick search brought up ldd for Unix.

JRL
  • 76,767
  • 18
  • 98
  • 146
5
  1. Add 'PHPIniDir "C:/php"' into the httpd.conf file.(provided you have your PHP saved in C:, or else give the location where PHP is saved.)
  2. Uncomment following 'extension=php_pgsql.dll' in php.ini file
  3. Uncomment ';extension_dir = "ext"' in php.ini directory
Chetan
  • 51
  • 1
  • 3
  • Please don't add "thank you" as an answer. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation), you will be able to [vote up questions and answers](http://stackoverflow.com/help/privileges/vote-up) that you found helpful. – Bono Mar 13 '15 at 13:23
  • 2
    but I tried giving a solution by adding a extra step for already present previous solution – Chetan Mar 13 '15 at 13:53
  • Ah alright. Would you consider improving your answer, because to me it was unclear that there was an extra step ;) – Bono Mar 13 '15 at 13:56
3

I had the same symptom in win7. I got this script:

<?php
    phpinfo();
    pg_connect("blah");

When I executed the phpinfo.php script via apache (http://localhost/phpinfo.php) then I got the error message: Call to undefined function pg_connect() in...

When I executed the same script from command line (php phpinfo.php) then I got the expected message: PHP Warning: pg_connect(): Unable to connect to PostgreSQL server: missing "=" after "blah"

In both cases the expected php.ini was used:

Loaded Configuration File   C:\Program Files (x86)\php\php.ini 

but the pgsql section was completely missing from the phpinfo in case of the apache-based execution and it was present in the command-line-based execution.

The solution was that I added the following line to the apache httpd.conf:

LoadFile "C:/Program Files (x86)/php/libpq.dll"

It seems that for some reason this file is not loaded automatically when apache runs the php script but it is loaded if I run the php script from the command line.

I hope it helps.

Csongor Halmai
  • 3,239
  • 29
  • 30
2

You have to follow these steps:

Open the php configuration file, which is located in the following directory

C: \ xampp \ php \ php.ini

Within that file search the extension section and uncomment the following lines

extension = php_pdo_pgsql.dll  
extension = php_pgsql.dll

and restart your apache

Vega
  • 27,856
  • 27
  • 95
  • 103
1

I also had this problem on OSX. The solution was uncommenting the extension = pgsql.so in php.ini.default and deleting the .default suffix, since the file php.ini was not there.

If you are using XAMPP, the php.ini file resides in /XAMPP/xampfiles/etc

Muzoora Savior
  • 529
  • 5
  • 16
user9548
  • 11
  • 1
1

install the package needed. if you use yum:

yum search pgsql

then look at the result and find anything that is something like 'php-pgsql' or something like that. copy the name and then:

yum install *paste the name of the package here*
Maya SC
  • 11
  • 3
1

For php 5.4 on Centos 6.10, we include these lines in php.ini

extension=/opt/remi/php54/root/usr/lib64/php/modules/pdo.so
extension=/opt/remi/php54/root/usr/lib64/php/modules/pgsql.so
extension=/opt/remi/php54/root/usr/lib64/php/modules/pdo_pgsql.so

It works.

Alf Newman
  • 11
  • 2
0

I encountered this error and it ended up being related to how PHP's extension_dir was loading.

If upon printing out phpinfo() you find that under the PDO header PDO drivers is set to no value, you may want to check that you are successfully loading your extension directory as detailed in this post:

https://stackoverflow.com/a/14786808/2578505

Community
  • 1
  • 1
quetzaluz
  • 1,071
  • 12
  • 17
0

On Gentoo, use USE flag postgres in /etc/portage/make.conf and re emerge "emerge php"

kemals
  • 216
  • 1
  • 7
0

For those of you who have this problem with PHP 5.6, you can use the following command:

yum install php56w-pgsql

For a list of more package names for PHP 5.6, open the following link and scroll down to packages:

PHP 5.6 on CentOS/RHEL 7.0 and 6.6 via Yum

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
SPRBRN
  • 2,406
  • 4
  • 35
  • 48
0

Uncommenting extension=php_pgsql.dll in the php.ini configuration files does work but, you may have to also restart your XAMPP server to finally get it working. I had to do this.

Adim Victor
  • 104
  • 6
0

The solution has to do with the fact that your the file holding your php configurations. i.e php.ini has uncommented the extension responsible for acting as the middleman between php and postgres, by placing a ";" in front of the statement "extension=pdo_pgsql"

Quick Fix

  1. Open the php.ini file in your favourite editor. (atom )
  2. Search for the line "extension=pdo_pgsql", which is under the "Dynamic Extensions" section. (a simple ctrl + f) would get you there quick.
  3. Remove the ";" in front of the line ";extension=pdo_pgsql".
  4. Restart your server.
  5. Go fix more errors like a rock star.
Geeky64
  • 29
  • 2
0

If you got php5.6 using the ppa repository http://ppa.launchpad.net/ondrej/php/ubuntu,

then you should install the package using:

sudo apt install php5.6-pgsql

Finally, if you use apache2, restart it:

sudo service apache2 restart

Raül Ojeda
  • 69
  • 1
  • 6
-1

In windows OS find this in php.ini "php_pgsql.dll" and remove the ";" in the extension then that's it :) Cheeers!