Thread: Help with pq++

Help with pq++

From
Aristide Aragon
Date:
Hello
I need to make a program that uses pq++, before starting I made a little program to see if I understood correctly how
touse pq++, however I have a problem: The program can't connect to the DB. 
My code looks like this:
#include <stdio.h>
#include <libpq++.h>

int main()
{
   PgDatabase db("dbname=mydb");
   if (!(db.ConnectionBad()))
   {
      printf("OK\n");
      exit(0);
   }
   else
   {
      printf("Error connecting\n");
      exit(1);
   }
}

And no matter what I do (I tried with the env variables PGHOST, PGPORT, PGDATABASE and PGUSER) the program always gives
me"Error connecting". 
Can somebody help? I was expecting to have problems, but not this early.

Aristide

Re: Help with pq++

From
Aristide Aragon
Date:
On Mon, Feb 26, 2001 at 01:21:43PM -0500, Belcher, Jim wrote:
> the code looks fine, is the user you run this code with
> in the database as a valid user for database "mydb"?

The user exists, but I don't know if for that database. How do I know?

Thanks

Aristide

> -----Original Message-----
> From: Aristide Aragon [mailto:aristide@lionking.org]
> Sent: Saturday, February 24, 2001 8:11 PM
> To: pgsql-general@postgresql.org
> Subject: Help with pq++
>
>
> Hello
> I need to make a program that uses pq++, before starting I made a little
> program to see if I understood correctly how to use pq++, however I have a
> problem: The program can't connect to the DB.
> My code looks like this:
> #include <stdio.h>
> #include <libpq++.h>
>
> int main()
> {
>    PgDatabase db("dbname=mydb");
>    if (!(db.ConnectionBad()))
>    {
>       printf("OK\n");
>       exit(0);
>    }
>    else
>    {
>       printf("Error connecting\n");
>       exit(1);
>    }
> }
>
> And no matter what I do (I tried with the env variables PGHOST, PGPORT,
> PGDATABASE and PGUSER) the program always gives me "Error connecting".
> Can somebody help? I was expecting to have problems, but not this early.
>
> Aristide

Re: Help with pq++

From
Aristide Aragon
Date:
On Mon, Feb 26, 2001 at 01:53:18PM -0500, Belcher, Jim wrote:
> Im not sure how to tell, but i had to to a "grant all on table 'such and
> such'"
> for a user to have access to that table.  You can also grant partial
> privleges
> such as insert etc.

I did a "GRANT all ON mydb TO public" and I still ge tthe error.
Is there no error log or something where I could check what is wrong?

Thanks

Aristide

> -----Original Message-----
> From: Aristide Aragon [mailto:aristide@lionking.org]
> Sent: Monday, February 26, 2001 1:51 PM
> To: Belcher, Jim
> Cc: pgsql-general@postgresql.org
> Subject: Re: Help with pq++
>
>
> On Mon, Feb 26, 2001 at 01:21:43PM -0500, Belcher, Jim wrote:
> > the code looks fine, is the user you run this code with
> > in the database as a valid user for database "mydb"?
>
> The user exists, but I don't know if for that database. How do I know?
>
> Thanks
>
> Aristide
>
> > -----Original Message-----
> > From: Aristide Aragon [mailto:aristide@lionking.org]
> > Sent: Saturday, February 24, 2001 8:11 PM
> > To: pgsql-general@postgresql.org
> > Subject: Help with pq++
> >
> >
> > Hello
> > I need to make a program that uses pq++, before starting I made a little
> > program to see if I understood correctly how to use pq++, however I have a
> > problem: The program can't connect to the DB.
> > My code looks like this:
> > #include <stdio.h>
> > #include <libpq++.h>
> >
> > int main()
> > {
> >    PgDatabase db("dbname=mydb");
> >    if (!(db.ConnectionBad()))
> >    {
> >       printf("OK\n");
> >       exit(0);
> >    }
> >    else
> >    {
> >       printf("Error connecting\n");
> >       exit(1);
> >    }
> > }
> >
> > And no matter what I do (I tried with the env variables PGHOST, PGPORT,
> > PGDATABASE and PGUSER) the program always gives me "Error connecting".
> > Can somebody help? I was expecting to have problems, but not this early.
> >
> > Aristide

Re: Help with pq++

From
Jose Manuel Lorenzo Lopez
Date:
Aristide Aragon schrieb:
>
> On Mon, Feb 26, 2001 at 01:53:18PM -0500, Belcher, Jim wrote:
> > Im not sure how to tell, but i had to to a "grant all on table 'such and
> > such'"
> > for a user to have access to that table.  You can also grant partial
> > privleges
> > such as insert etc.
>
> I did a "GRANT all ON mydb TO public" and I still ge tthe error.
> Is there no error log or something where I could check what is wrong?

Hello Aristide,

I guess it's not your program, which seems okay, but your user.
Please check if the user which is supposed to execute the program
is added to the DB. If not do a createuser for the mentioned user.

To be more sure add the following lines in the else-branch of your
source-code:

// Get a detailed message about the error ocurred
fprintf(stderr, "Error Message: %s", db.ErrorMessage());

or check the postmaster log after executing the program.

Hope this will help you and give you confidence back for programming
apps with postgresql! :)

Best Regards / Un saludo / Mit freundlichen Grüßen / Cordiali Saluti

José Manuel Lorenzo López

--
**********************************************************************
** José Manuel Lorenzo López                                        **
**                                                                  **
** ICA Informationssysteme Consulting & Anwendungsgesellschaft mbH  **
** Dept. SAP Basis R/3  VBue                                        **
**                                                                  **
** e-mail to: jose-manuel.lorenzo-lopez@conti.de                    **
**********************************************************************
--
**********************************************************************
** José Manuel Lorenzo López                                        **
**                                                                  **
** ICA Informationssysteme Consulting & Anwendungsgesellschaft mbH  **
** Dept. SAP Basis R/3  VBue                                        **
**                                                                  **
** phone: (+49511) 938-2961                                         **
** fax  : (+49511) 938-2270                                         **
** e-mail to: jose-manuel.lorenzo-lopez@conti.de                    **
**********************************************************************

Re: Re: Help with pq++

From
Aristide Aragon
Date:
On Tue, Feb 27, 2001 at 10:09:49AM +0100, Jose Manuel Lorenzo Lopez wrote:
> Aristide Aragon schrieb:
> > On Mon, Feb 26, 2001 at 01:53:18PM -0500, Belcher, Jim wrote:

Hello

OK, I got the problem sorted out. The error message I received was "Error Message: FATAL 1:  Database "mydb" does not
existin the system catalog." 
That makes sense, the machine running this program doesn't have a mydb database, however I set the enviroment variable
PGHOSTto some other computer (and I just tested if I run the program there it returns OK). 
While I won't be running this program over a network, I do would like to know what's going on.
How would I do it so that my program accesses the DB in another host? The documentation said (or at least that's what I
understood)to export the PGHOST variable to the address of that other machine. 
I'll be running my program web-based, if I wanted it to connect to some other host then, how'd I do it?

Thanks

Aristide

> Hello Aristide,
>
> I guess it's not your program, which seems okay, but your user.
> Please check if the user which is supposed to execute the program
> is added to the DB. If not do a createuser for the mentioned user.
>
> To be more sure add the following lines in the else-branch of your
> source-code:
>
> // Get a detailed message about the error ocurred
> fprintf(stderr, "Error Message: %s", db.ErrorMessage());
>
> or check the postmaster log after executing the program.
>
> Hope this will help you and give you confidence back for programming
> apps with postgresql! :)
>
> Best Regards / Un saludo / Mit freundlichen Grüßen / Cordiali Saluti
>
> José Manuel Lorenzo López
>
> --
> **********************************************************************
> ** José Manuel Lorenzo López                                        **
> **                                                                  **
> ** ICA Informationssysteme Consulting & Anwendungsgesellschaft mbH  **
> ** Dept. SAP Basis R/3  VBue                                        **
> **                                                                  **
> ** fax  : (+49511) 938-2270                                         **
> ** e-mail to: jose-manuel.lorenzo-lopez@conti.de                    **
> **********************************************************************
>

Re: Re: Help with pq++

From
Aristide Aragon
Date:
On Tue, Feb 27, 2001 at 10:09:49AM +0100, Jose Manuel Lorenzo Lopez wrote:
> Aristide Aragon schrieb:
> > On Mon, Feb 26, 2001 at 01:53:18PM -0500, Belcher, Jim wrote:

Hello

OK, I got the problem sorted out. The error message I received was "Error Message: FATAL 1:  Database "mydb" does not
existin the system catalog." 
That makes sense, the machine running this program doesn't have a mydb database, however I set the enviroment variable
PGHOSTto some other computer (and I just tested if I run the program there it returns OK). 
While I won't be running this program over a network, I do would like to know what's going on.
How would I do it so that my program accesses the DB in another host? The documentation said (or at least that's what I
understood)to export the PGHOST variable to the address of that other machine. 
I'll be running my program web-based, if I wanted it to connect to some other host then, how'd I do it?

Thanks

Aristide

> Hello Aristide,
>
> I guess it's not your program, which seems okay, but your user.
> Please check if the user which is supposed to execute the program
> is added to the DB. If not do a createuser for the mentioned user.
>
> To be more sure add the following lines in the else-branch of your
> source-code:
>
> // Get a detailed message about the error ocurred
> fprintf(stderr, "Error Message: %s", db.ErrorMessage());
>
> or check the postmaster log after executing the program.
>
> Hope this will help you and give you confidence back for programming
> apps with postgresql! :)
>
> Best Regards / Un saludo / Mit freundlichen Grüßen / Cordiali Saluti
>
> José Manuel Lorenzo López
>
> --
> **********************************************************************
> ** José Manuel Lorenzo López                                        **
> **                                                                  **
> ** ICA Informationssysteme Consulting & Anwendungsgesellschaft mbH  **
> ** Dept. SAP Basis R/3  VBue                                        **
> **                                                                  **
> ** fax  : (+49511) 938-2270                                         **
> ** e-mail to: jose-manuel.lorenzo-lopez@conti.de                    **
> **********************************************************************
>

Re: Re: Help with pq++

From
Jose Manuel Lorenzo Lopez
Date:

Aristide Aragon schrieb:

> OK, I got the problem sorted out. The error message I received was
> "Error Message: FATAL 1:  Database "mydb" does not exist in the
> system catalog." That makes sense, the machine running this program
> doesn't have a mydb database, however I set the enviroment variable
> PGHOST to some other computer (and I just tested if I run the program
> there it returns OK).
> While I won't be running this program over a network, I do would like
> to know what's going on. How would I do it so that my program accesses
> the DB in another host? The documentation said (or at least that's what
> I understood) to export the PGHOST variable to the address of that other
> machine. I'll be running my program web-based, if I wanted it to
> connect to some other host then, how'd I do it?

Hello Aristide,

You can pass the hostname of the machine you want to connect to as
parameter when starting the program accesing the variables argv and
argc.

You have to define the main-function this way
int main(int argc, char *argv[])

and then pass the variables argc and argv to the constructor of the
PG-interface when creating the object.

TIP: If you are already using C++ you may want to create a class that
handles the DB-connections. You can save there the actual connection
parameters and create there connection string to pass to the contructor
of PgDatabase. That's the way I do it. :)

Best Regards / Un saludo / Mit freundlichen Grüßen / Cordiali Saluti

José Manuel Lorenzo López

--
**********************************************************************
** José Manuel Lorenzo López                                        **
**                                                                  **
** ICA Informationssysteme Consulting & Anwendungsgesellschaft mbH  **
** Dept. SAP Basis R/3  VBue                                        **
**                                                                  **
** e-mail to: jose-manuel.lorenzo-lopez@conti.de                    **
**********************************************************************

Re: Help with pq++

From
Patrick Welche
Date:
On Sat, Feb 24, 2001 at 07:11:19PM -0600, Aristide Aragon wrote:
> Hello
> I need to make a program that uses pq++, before starting I made a little program to see if I understood correctly how
touse pq++, however I have a problem: The program can't connect to the DB. 
> My code looks like this:
> #include <stdio.h>
> #include <libpq++.h>
>
> int main()
> {
>    PgDatabase db("dbname=mydb");
>    if (!(db.ConnectionBad()))
>    {
>       printf("OK\n");
>       exit(0);
>    }
>    else
>    {
>       printf("Error connecting\n");
>       exit(1);
>    }
> }
>
> And no matter what I do (I tried with the env variables PGHOST, PGPORT, PGDATABASE and PGUSER) the program always
givesme "Error connecting". 
> Can somebody help? I was expecting to have problems, but not this early.

You're right - that seems early. Can you connect to it with psql mydb?

Patrick