Re: Connection string - Mailing list pgsql-general

From Harpreet Dhaliwal
Subject Re: Connection string
Date
Msg-id d86a77ef0608100857k546bc1dbld9e76ff2219fe553@mail.gmail.com
Whole thread Raw
In response to Re: Connection string  (Michael Fuhr <mike@fuhr.org>)
Responses Re: Connection string  (Michael Fuhr <mike@fuhr.org>)
Re: Connection string  (Michael Meskes <meskes@postgresql.org>)
List pgsql-general
This is a simple code snippet that i've written to check if i can connect to the postgres database server residing at IP 192.168.0.123. DB name is xyz
Also, user account jsb has the access to the database xyz.
------------------------------------------------------------
#include <stdio.h>
 
EXEC SQL INCLUDE sqlca
 
int main ()
{
     EXEC SQL BEGIN DECLARE SECTION;
         char abc[20];
     EXEC SQL END DECLARE SECTION;
 
     EXEC SQL CONNECT TO  'tcp:postgresql://192.168.0.123/xyz' USER jsb
 
    printf("Error code is: %d ", SQLCODE);
 
 
}
 
---------------------------------------------------------------------------------------------------------------------------
 
The error code that it gives me is -402 that means it could not establish the connection.
 
Don't know whats going wrong.
 
Thanks,
Harpreet.
 

 
On 8/10/06, Michael Fuhr <mike@fuhr.org> wrote:
On Thu, Aug 10, 2006 at 12:02:24AM -0400, Harpreet Dhaliwal wrote:
> I already read that documentation.
>
> My ECPG code for connecting to the DB server is:
>
> EXEC SQL CONNECT TO 192.168.1.100:/xyz

That format isn't shown in the documentation; the ecpg preprocessor
should fail with a syntax error if you try using it.

> i also tried
>
> tcp:postgresql://192.168.1.100[:*port*][/*dbname*][?*options*]

The above is probably what you need, but without seeing the exact
code you tried it's hard to say why it's not working.

> unix:postgresql://*192.168.1.100*[:*port*][/*dbname*][?*options*]

The ecpg preprocessor shouldn't allow this -- it should fail with
an error like "unix domain sockets only work on 'localhost' but not
on '192.168.1.100'".

> but unfortunately it say DB doesn't exist.

Are you sure the database exists?  Can you connect to it with
psql?

> I don't know the right way to use IP addresses while connecting to a
> postgres DB using ECPG.

If you have a server on 192.168.1.100 listening on the default port
(5432, or whatever PGPORT is set to) and you want to connect to a
database named "mydb" on that server, then the following should
work:

EXEC SQL CONNECT TO tcp:postgresql://192.168.1.100/mydb;

If the database is listening on another port, say 12345, then
this should work:

EXEC SQL CONNECT TO tcp:postgresql://192.168.1.100:12345/mydb;

If you're getting 'database "mydb" does not exist' errors then try
connecting with psql and make sure the database really does exist.
If you still have trouble then please post a minimal but complete
program so we can see everything you're doing.

--
Michael Fuhr

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Tuning to speed select
Next
From: "DEV"
Date:
Subject: No comerr32.dll and krb5_32.dll on WIN32 build