Thread: HELP FOR POSTGRESQL NEEDED...

HELP FOR POSTGRESQL NEEDED...

From
ozric tentacle
Date:
<p>Hello.I am experiencing a quite strange behavior with my <strong>postgreSQL database</strong>.<p>I have edited the
file<strong>pg_hba.conf</strong> like this:<p># TYPE     DATABASE    IP_ADDRESS    MASK               AUTH_TYPE 
AUTH_ARGUMENT<br/>local      all                                                                       password<br
/>host      all         127.0.0.1     255.255.255.255                   password<p>in order to require user
authentication.Thisworks perfectly when I connect to the database with <strong>psql</strong> or
<strong>pgaccess</strong>and I'm able to perform<p>all kinds of queries.<p>However when I try to connect to the
databasewith <strong>C++</strong> even though it allows me to connect (providing the correct username &
password)<p><strong>allthe queries fail...</strong>The C++ code I use is the following:<p><br />#include
<stdio.h><br/>#include <stdlib.h><br />#include <string.h><br />#include <iostream.h><br
/>#include<string><p>#include "libpq++.h"<p>using namespace std;<p><br />int main()<p>{<br />  string
login,pwd;<br/>  char name[10];<br />  char query_string[256];<p>  cout << ">username:";<br />  cin >>
login;<br/>  cout << ">password:";<br />  cin >> pwd;<p>  cout<<"user to search:";<br />  cin
>>name;<p><br />  // Open the connection to the database and make sure it's OK<p>  PgDatabase
data("dbname=goumero");<p><br/>  char       *pghost,*pgport,*pgoptions,*pgtty;<br />  char       *dbName;<p>   pghost =
NULL;             /* host name of the backend server */<br />   pgport = NULL;              /* port of the backend
server*/<br />   pgoptions = NULL;         /* special options to start up the backend server */<br />   pgtty =
NULL;              /* debugging tty for the backend server */<br />   dbName = "goumero";<p>   PGconn* conn;<p>   conn
= PQsetdbLogin(pghost,pgport,pgoptions,pgtty,dbName,login.c_str(),pwd.c_str()) ;<p>  if (PQstatus(conn) ==
CONNECTION_BAD) {<br />        fprintf(stderr, "Connection to database '%s' failed.\n", dbName);<br />       
fprintf(stderr,"%s", PQerrorMessage(conn));<br />        return 1;<br />  }<br />  else<br />      //Connection
successful...<p><br/>      data.Exec("BEGIN WORK");<br />      data.Exec("LOCK TABLE account in ACCESS EXCLUSIVE 
MODE");<p><br/>            sprintf(query_string,                               // create an SQL query string<br
/>                   "SELECT *   \<br />                     FROM account \<br />                     WHERE name='%s'"
,name);<br />              if ( !data.ExecTuplesOk(query_string) )             // send the query<br />            {<br
/>               cerr << "query failed." << endl;<br />                exit(1);<br />           
}<p>           for (int i=0; i < data.Tuples(); i++)               // loop through all rows returned<br
/>                   cout << data.GetValue(i,0) << endl;   // print the value
returned<p>/**************************************************************************************/<p>     
data.Exec("COMMITWORK");<br />      PQfinish(conn);<p>return 0;<p>} // End main()<p><strong><font
color="blue"><u>Note</u></font></strong>:Thesame query above is correctly executed when I connect with
<strong>psql</strong>.<p><strong><u>Also if I remove the user authentication stuff then the same code is
executed correctly.</u></strong><p><strong><fontcolor="red">I would be grateful if you helped me - I'm
desperate....</font></strong><p>  <p> <p><br/> <p> <p><br /> <p> <p><br /><hr size="1" /><b>Do You Yahoo!?</b><br
/>Αποκτήστετην δωρεάν σας @yahoo.gr διεύθυνση στο <a href="http://www.otenet.gr/">Yahoo! Mail</a>.<br /> 

Re: HELP FOR POSTGRESQL NEEDED...

From
Steve Howe
Date:
Hello ozric,

Saturday, July 6, 2002, 9:22:44 AM, you wrote:


ot> Hello.I am experiencing a quite strange behavior with my
ot> postgreSQL database.

ot> I have edited the file pg_hba.conf like this:

ot> # TYPE     DATABASE    IP_ADDRESS    MASK               AUTH_TYPE  AUTH_ARGUMENT
ot> local      all                                                                       password
ot> host       all         127.0.0.1     255.255.255.255                   password

ot> in order to require user authentication.This works perfectly when
ot> I connect to the database with psql or pgaccess and I'm able to
ot> perform

ot> all kinds of queries.

ot> However when I try to connect to the database with C++ even though
ot> it allows me to connect (providing the correct username &
ot> password)

ot> all the queries fail...The C++ code I use is the following:

Did you start the postmaster with -i parameter so that it allows
TCP/IP connections ?
Run a nmap localhost command to check if your 5432 port is open. If
PostgreSQL is running and accepting connections, it should be listed
as open.
Psql will connect through unix sockets, not as TCP sockets.

------------- 
Best regards,Steve Howe                           mailto:howe@carcass.dhs.org