Jump to content

[SOLVED] can't connect to postgres database with php


Recommended Posts

Just trying a simple connection for now, but when I try to view it via browser, it says that pg_connect is an undefined function.  I'm using php5

 

<?
$connection=pg_connect("host=***.***.***.*** dbname=**** user=**** password=*********************");

if($connection){
        echo "Connected";
}else{
        echo "Unable to connect";
}
?>

 

Any idea why pg_connect wouldn't be recognized?

Do you have the module loaded?  You need to include the postgres extension in php.ini and restart apache.  You can check with phpinfo().

 

Pardon the lack of knowledge...but I found this section in php.ini.  Is this what I'm looking for?

 

[PostgresSQL]
; Allow or prevent persistent links.
pgsql.allow_persistent = On

; Detect broken persistent links always with pg_pconnect().
; Auto reset feature requires a little overheads.
pgsql.auto_reset_persistent = Off

; Maximum number of persistent links.  -1 means no limit.
pgsql.max_persistent = -1

; Maximum number of links (persistent+non persistent).  -1 means no limit.
pgsql.max_links = -1

; Ignore PostgreSQL backends Notice message or not.
; Notice message logging require a little overheads.
pgsql.ignore_notice = 0

; Log PostgreSQL backends Noitce message or not.
; Unless pgsql.ignore_notice=0, module cannot log notice message.
pgsql.log_notice = 0

Find the section called Dynamic Extensions and uncomment the line that contains "extension=php_pgsql.dll" (Windows) or "extension=php.pgsql.so" (Linux).  Restart Apache and run a script that just has these lines.

 

phpinfo();

?>

 

You should be able to find a list of all the extensions that you have enabled and installed.  If you see postgre than you should be able to use postgre.

Find the section called Dynamic Extensions and uncomment the line that contains "extension=php_pgsql.dll" (Windows) or "extension=php.pgsql.so" (Linux).  Restart Apache and run a script that just has these lines.

 

<?php

phpinfo();

?>

 

You should be able to find a list of all the extensions that you have enabled and installed.  If you see postgre than you should be able to use postgre.

 

 

I found the Dynamic Extensions section, but there is no line that says extension=php.pgsql.so.  Is it safe to just add the line or am I missing a package of some sort?

So I assume you're using Linux since you referenced the .so extension.  To answer your question, yes, add that line in your php.ini.  You can check to ensure you have "php.pgsql.so" in your extension directory by going to the "extension_dir" (something like that) path.  If not you will have to add it, I've never installed postgres before so I'm not sure what's configured by default.

Depending on which linux distribution you are using, you can probably install a package that will do this.  On my system there is a php package, a postgresql package, AND another package which makes postgresql available from php.  That last package will install itself into the apache php.ini if apache is installed.  If it doesn't, Maq's instructions ought to fix it.

 

Which distribution are you using?

Using Ubuntu Server 9.04

 

I added the extension=php.pgsql.so line in php.ini.  No immediate change, still says pg_connect() is not a defined function.  There is a s sub-section in the "Paths and Directories" section...

 

; Directory in which the loadable extensions (modules) reside.
; extension_dir = "./"

 

Does this need to be uncommented?

I added the extension=php.pgsql.so line in php.ini.  No immediate change, still says pg_connect() is not a defined function

 

Did you restart apache after you made changes to php.ini?  You can check to see if postgres exists by running a script with phpinfo() in it.

 

Does this need to be uncommented?

 

If your extensions reside in the same directory (./) as php.ini then yes.  This line just specifies where your extensions exist i.e. php.pgsql.so

I added the extension=php.pgsql.so line in php.ini.  No immediate change, still says pg_connect() is not a defined function

 

Did you restart apache after you made changes to php.ini?  You can check to see if postgres exists by running a script with phpinfo() in it.

 

Does this need to be uncommented?

 

If your extensions reside in the same directory (./) as php.ini then yes.  This line just specifies where your extensions exist i.e. php.pgsql.so

 

 

Figured it out.  I did apt-get install php5-pgsql and that fixed the problem.  Perhaps this is the 3rd package that was mentioned?

I added the extension=php.pgsql.so line in php.ini.  No immediate change, still says pg_connect() is not a defined function

 

Did you restart apache after you made changes to php.ini?  You can check to see if postgres exists by running a script with phpinfo() in it.

 

Does this need to be uncommented?

 

If your extensions reside in the same directory (./) as php.ini then yes.  This line just specifies where your extensions exist i.e. php.pgsql.so

 

 

Figured it out.  I did apt-get install php5-pgsql and that fixed the problem.  Perhaps this is the 3rd package that was mentioned?

 

Nice.  I was just reading a tutorial that goes that route to install the postgres package.  It puts the package in your extension_dir, adds the Dynamic Extension entry in your php.ini, and handles some other dependency issues.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.