LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 08-04-2010, 07:28 PM   #1
kapshure
LQ Newbie
 
Registered: Mar 2010
Posts: 29

Rep: Reputation: 1
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"

Last edited by kapshure; 08-04-2010 at 07:53 PM.
 
Old 08-04-2010, 07:53 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,679

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
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.
 
Old 08-04-2010, 07:55 PM   #3
kapshure
LQ Newbie
 
Registered: Mar 2010
Posts: 29

Original Poster
Rep: Reputation: 1
@michaelk

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.

Last edited by kapshure; 08-04-2010 at 08:04 PM.
 
Old 08-04-2010, 08:48 PM   #4
themanwhowas
Member
 
Registered: Nov 2005
Distribution: CentOS 5, Fedora 23
Posts: 218

Rep: Reputation: 29
Hi,

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.1USERPASSWORD) OR die('Couldn\'t access database server');
mysql_select_db(testdb) OR die('Couldn\'t access database'); 
 
Old 08-04-2010, 09:19 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,679

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
BTW php CLI is independent of apache.

postgres is the postgresql superuser and assuming you have not added any users is the only one that can connect to a database.
 
Old 08-04-2010, 11:35 PM   #6
kapshure
LQ Newbie
 
Registered: Mar 2010
Posts: 29

Original Poster
Rep: Reputation: 1
@themanwhowas

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.
 
Old 08-05-2010, 12:40 PM   #7
kapshure
LQ Newbie
 
Registered: Mar 2010
Posts: 29

Original Poster
Rep: Reputation: 1
OK I went back and modified the "testdb.php" file:

<?php
$connection = pg_connect ("host=localhost dbname=test user=jw password=password");
?>


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.
 
Old 08-06-2010, 01:59 PM   #8
kapshure
LQ Newbie
 
Registered: Mar 2010
Posts: 29

Original Poster
Rep: Reputation: 1
OK, I have to come back here and add something.

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


here is testdb.php again:

Quote:
<?php
$connection = pg_connect ("host=localhost dbname=salon user=postgres password=test");
?>

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.
 
Old 08-06-2010, 03:58 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,679

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
The actual conf file is located here:
/var/lib/pgsql/data
 
Old 08-06-2010, 04:20 PM   #10
kapshure
LQ Newbie
 
Registered: Mar 2010
Posts: 29

Original Poster
Rep: Reputation: 1
@michaelk

i added:

Quote:
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.

Last edited by kapshure; 08-06-2010 at 04:21 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
test slow connectivity mario.almeida Linux - Networking 6 03-15-2010 03:53 AM
[SOLVED] Connectivity test fails OptusWrlssBrdbnd, configuration ok widda Mandriva 15 01-21-2010 05:32 AM
LXer: Xen 3.2 on CentOS 5.2 x86_64 / RHEL 5.2 x86_64 LXer Syndicated Linux News 0 07-12-2008 05:51 PM
how to test network programs with out LAN connectivity on FC5 morningkiran Linux - Networking 3 02-07-2007 02:38 AM
postgresql tomcat database connectivity error dsids Linux - General 1 04-20-2006 01:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration