pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
PostgresSQL server connectiong problem with C-language...
2+ messages / 2 participants
[nested] [flat]

* PostgresSQL server connectiong problem with C-language...
@ 2007-10-15 06:13  Madhu Sudhana Rao <madhu542@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Madhu Sudhana Rao @ 2007-10-15 06:13 UTC (permalink / raw)
  To: pgsql-interfaces

Hi,

I wrote a C-program for connecting to postgresSQL server and get the
data from the database.

The program is as fallows:-
Code: ( c )

#include <stdio.h>
#include <stdlib.h>
#include "libpq-fe.h"                                   /* libpq header file */
int
main()
{
          char        state_code[3];
          char        query_string[256];                      /* holds
constructed SQL query */
          PGconn     *conn;                                   /* holds
database connection */
          PGresult   *res;
          int         i;
          conn = PQconnectdb("dbname=test");                  /*
connect to the database */
          if (PQstatus(conn) == CONNECTION_BAD)               /* did
the database connection fail? */
          {
              fprintf(stderr, "Connection to database failed.\n");
              fprintf(stderr, "%s", PQerrorMessage(conn));
              exit(1);
          }
          printf("Enter a state code: ");                     /*
prompt user for a state code */
          scanf("%2s", state_code);
          sprintf(query_string,                               /*
create an SQL query string */
                  "SELECT name \
                   FROM statename \
                   WHERE code = '%s'", state_code);
           res = PQexec(conn, query_string);                   /* send
the query */
           if (PQresultStatus(res) != PGRES_TUPLES_OK)         /* did
the query fail? */
           {
               fprintf(stderr, "SELECT query failed.\n");
               PQclear(res);
               PQfinish(conn);
               exit(1);
           }
           for (i = 0; i < PQntuples(res); i++)                /* loop
through all rows returned */
                printf("%s\n", PQgetvalue(res, i, 0));          /*
print the value returned */
           PQclear(res);                                       /* free result */
           PQfinish(conn);                                     /*
disconnect from the database */
           return 0;
        }

I changed the postgresql.conf and pg_hba.conf files.

listen_addresses = '*'                  # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost', '*' = all
                                        # (change requires restart)
port = 5432                             # (change requires restart)
max_connections = 100
----------------------------------------------------------------------------

# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
local   all         all                               trust

# The same using local loopback TCP/IP connections.
#
# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
host    all         all         0.0.0.0/0          trust




every time I a getting the same error when I was trying to run the program:-

Connection to database failed.
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?



Thanks for reading this long mail.
could any one plz help me how to solve this problem...


Thanks & Regards...
Madhu.



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: PostgresSQL server connectiong problem with C-language...
@ 2007-10-15 11:13  Jonah H. Harris <jonah.harris@gmail.com>
  parent: Madhu Sudhana Rao <madhu542@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Jonah H. Harris @ 2007-10-15 11:13 UTC (permalink / raw)
  To: Madhu Sudhana Rao <madhu542@gmail.com>; +Cc: pgsql-interfaces

On 10/15/07, Madhu Sudhana Rao <madhu542@gmail.com> wrote:
> I wrote a C-program for connecting to postgresSQL server and get the
> data from the database.
> ...
> Connection to database failed.
> could not connect to server: No such file or directory
> Is the server running locally and accepting
> connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

Is the server running?  Does the log show anything unusual?  Did you
change any of the unix_socket_* configuration parameters?  Do you have
a database named test (although that would give a different error
message).  Can you connect with psql?

As far as libpq is concerned, the best thing to do is start with is
the libpq example code and work from there.

Also, sorry to be picky about your code, but your conditional is:

if (PQstatus(conn) == CONNECTION_BAD)

It's generally better to make the conditional check for an alteration
to the expected result:

if (PQstatus(conn) != CONNECTION_OK)

It just makes it easier to understand what should've happened.

-- 
Jonah H. Harris, Sr. Software Architect | phone: 732.331.1324
EnterpriseDB Corporation                | fax: 732.331.1301
499 Thornall Street, 2nd Floor          | jonah.harris@enterprisedb.com
Edison, NJ 08837                        | http://www.enterprisedb.com/



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2007-10-15 11:13 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-10-15 06:13 PostgresSQL server connectiong problem with C-language... Madhu Sudhana Rao <madhu542@gmail.com>
2007-10-15 11:13 ` Jonah H. Harris <jonah.harris@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox