Thread: SQL error: could not connect to database

SQL error: could not connect to database

From
"Juba, Salahaldin I."
Date:
I am trying to connect to  database using ECPG  called 'database' - please  see the code below-.  When I am excuting
thecode I am getting this message  

Fatal error
SQL error: could not connect to database "database" on line 22
Fatal error
SQL error: connection "NULL" does not exist on line 23


How can I make the ECPG access my database. I have already configured my database to trust all local connections and I
canlog in to postgresql without password prompt using the psql tool. where did I make a mistake and what are the
configurationI need to do 

I am using libecpg 6 and postgresql 8.4.4

Best,


#include <iostream>

using namespace  std;

EXEC SQL whenever sqlwarning sqlprint;
EXEC SQL whenever sqlerror do GiveUp();

void GiveUp()
{
fprintf(stderr, "Fatal error\n");
sqlprint();
}
int main()
{

    EXEC SQL BEGIN DECLARE SECTION;
        char id [] = "database";
    EXEC SQL END DECLARE SECTION;
    EXEC SQL CONNECT TO :id;
    EXEC SQL CREATE USER tom WITH PASSWORD 'myPassword';
    EXEC SQL DISCONNECT ALL;

return 0;
}

Re: SQL error: could not connect to database

From
Kenichiro Tanaka
Date:
Hi.

At first,I think you lack some settings.
(eg. pg_hba.conf,listen_addresses or restarting)
But you say "I can log in to postgresql without password prompt using
the psql tool".

I can image some case,but we have to get some more information.

So I'd like you to try the following program.This program works fine on
my environment.
I believe this test can indicate which makes problem your environment or
your program.

Can you try this?


void GiveUp()
{
fprintf(stderr, "Fatal error\n");
sqlprint();
}


main ()
{
   /* declare variables*/
   exec sql begin declare section;
   char v_datcollate[256];
   char id [] = "database";
   exec sql end declare section;
   exec sql whenever sqlerror do GiveUp();

   /*connection*/
   exec sql  connect to :id;

   exec sql set autocommit = on;
   exec sql begin work;
   /* declare cursor */
   exec sql declare c_db cursor for select datcollate from pg_database
where datname=:id;

   /*1.select test*/
   printf("1.Select test starts!! \n");
   exec sql open c_db;
   exec sql fetch in c_db into :v_datcollate;
   printf("datcollate = %s \n",v_datcollate);
   exec sql close c_db;
   exec sql disconnect;
   return(0);
}



(2010/06/23 3:13), Juba, Salahaldin I. wrote:
> I am trying to connect to  database using ECPG  called 'database' - please  see the code below-.  When I am excuting
thecode I am getting this message 
>
> Fatal error
> SQL error: could not connect to database "database" on line 22
> Fatal error
> SQL error: connection "NULL" does not exist on line 23
>
>
> How can I make the ECPG access my database. I have already configured my database to trust all local connections and
Ican log in to postgresql without password prompt using the psql tool. where did I make a mistake and what are the
configurationI need to do 
>
> I am using libecpg 6 and postgresql 8.4.4
>
> Best,
>
>
> #include<iostream>
>
> using namespace  std;
>
> EXEC SQL whenever sqlwarning sqlprint;
> EXEC SQL whenever sqlerror do GiveUp();
>
> void GiveUp()
> {
> fprintf(stderr, "Fatal error\n");
> sqlprint();
> }
> int main()
> {
>
>     EXEC SQL BEGIN DECLARE SECTION;
>         char id [] = "database";
>     EXEC SQL END DECLARE SECTION;
>     EXEC SQL CONNECT TO :id;
>     EXEC SQL CREATE USER tom WITH PASSWORD 'myPassword';
>     EXEC SQL DISCONNECT ALL;
>
> return 0;
> }
>
>