[SOLVED] Trying to test PHP connectivity to Postgresql 8.3.9 (CentOS 5.5 x86_64)
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Trying to test PHP connectivity to Postgresql 8.3.9 (CentOS 5.5 x86_64)
hello. I'm learning via baptism by fire method here, and need to test PHP connectivity to a local postgresql 8.3.9 setup. I have got PHP and Postgres both working; I can pull up a test PHP page via web browser, and I can login to postgres via the command line.
What I can't figure out is how to test PHP connectivity to the local DB.
I found this test script:
Code:
<?php
$conn = pg_connect ('dbname=template1');
?>
which I've named, testdb.php - when I run ./testdb.php from the CLI though, I get:
Code:
./testdb.php: line 2: ?php: No such file or directory
./testdb.php: line 3: syntax error near unexpected token `"dbname=template1"'
./testdb.php: line 3: `$database=pg_connect ("dbname=template1");'
I've seen a LOT of pages describing the internal code of the PHP file, but as a newb to this, I don't know how to execute this test.
advice please? ive ran chmod 755 against the file to verify permissions, but this didnt effect the results. I've also done a "chmod +x testdb.php"
Code:
PHP 5.1.6 (cli) (built: Mar 31 2010 02:39:17)
[EDIT]: OK. DUH. I realized I wasn't doing this:
Code:
php testdb.php
but when I do, I get this error message now:
"PHP Fatal error: Call to undefined function pg_connect() in /var/www/html/testdb.php on line 3"
There are two methods to execute scripts via command line i.e
php test_script_php
or adding a first line shebang which tells bash (or whatever shell you are running) what interpreter to use.
#!/usr/bin/php
With the second case you need to set the executable permissions for your script via the chmod command.
BTW this also true for bash scripts, perl scripts and any other program that requires an interpreter. Of course replacing
php and #!/usr/bin/php with the correct interpreter.
hey thanks for the quick reply. I was editing my post when you were replying, as I realized I had forgot to run what you said, both the "php testdb.php" and then making the file executable, via "chmod +x"
please see the PHP Fatal error though. I haven't had a chance to google this yet. my apologies if it shows up as 1st entry
[EDIT]:
I've gotten a step closer. I didnt have php-pgsql.x86_64 installed. YUM'd that, restart HTTPD, now I'm getting an auth error for root. getting closer.
I believe for cli php usage, you should be using "php -f testdb.php". for some reason i have had many troubles with shebangs when using php.
Also, for MySQL you need to supply more than just the DB name. Probably the same with Postgress
PHP Code:
$con = mysql_connect(127.0.0.1, USER, PASSWORD) OR die('Couldn\'t access database server'); mysql_select_db(testdb) OR die('Couldn\'t access database');
I actually did try this test earlier today with db, username, hostname - however, I wasnt using php testdb.php. I will try that first thing tomorrow. Thanks again for your input.
@michaelk
ok that explains the error mssg that I got complaining about the root user acct. so I need to throw username into the pg_connect parameters.
i can login via CLI to the 'test' database with the 'jw' user without any issues - but I receive this when executing 'php testdb.php':
PHP Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: Ident authentication failed
then I found online that I should inspect pg_hba.conf - so I did. I wound up relaxing the security so I can just verify this test (the test is to actually show that PHP, mod_perl, postgres and apache can live and work harmoniously together in a monolithic binary install)
local all all trust
host all all 0.0.0.0 0.0.0.0 trust
Now, once I did this, I get no errors when I execute: 'php testdb.php' - I echoed the return status and got a "0", but the postgresql-x-x-x_x.log doesn't really validate that I successfully connected to the 'test' db w/ the 'jw' user account. Is there another quick way to verify this?
I suppose I could add tables, data, and then plug in a query to return the # of rows, etc.. I'm just really trying to do a simple connectivity test and if I've succeeded, then I'm happy.
Thanks so much for anyone's patience who is viewing and replying to this.
I have created another test VM, loaded up postgresql 8.1.21-1.el5_5.1 via yum. Am using the same testdb.php file, and I am hitting this error message again:
PHP Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: Ident authentication failed for user "postgres" in /var/www/html/testdb.php on line 2
I went back and ran ALTER USER postgres with password 'test'; to just verify that I had the right password, and I get the same results. I can login to pgsql from the command line, but cannot run this testdb.php script
any reason pg_hba.conf was not found loaded in this setup? meaning, only the pg_hba.conf.sample file was found.
local all all trust
host all all 0.0.0.0 0.0.0.0 trust
to pg_hba.conf, restarted postgresql, and the "php testdb.php" didnt return any errors.
im just curious, why when i ran "locate pg_hba.conf" it returned nothing.
anyways. thanks for your help.
p.s. - anyway you can take a look at the other thread I posted re: testing connectivity b/t mod_perl > mysql. I'm super confused on how to conduct that. seems like i get the wrong search terms in google, and once i get the query right, the answer pops out, but I cant seem to tease the answer(s) i need out of my searches so far.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.