By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,374 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,374 IT Pros & Developers. It's quick & easy.

PostgresSQL server connectiong problem with C-language...

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

The program is as fallows:-

#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;
}


every time I a getting the same error:-

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"?

could any plz help me how to solve this problem...

Thanks & Regards...
Oct 12 '07 #1
Share this Question
Share on Google+
7 Replies


Expert 100+
P: 700
I wrote C-program for connecting to postgresSQL server and get the data from the database.

The program is as fallows:-

#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;
}


every time I a getting the same error:-

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"?

could any plz help me how to solve this problem...

Thanks & Regards...

Is the serwer runnig and listening on port 5432?
Oct 12 '07 #2

P: 17
How to check whether server is running on port 5432 or not...
if it is not running on it how to run on that port...

please tell me because I am a new bee to this postgresql... and I am trying this program from last 3 days...

Thanks & Regards...
Is the serwer runnig and listening on port 5432?
Oct 12 '07 #3

Expert 100+
P: 700
How to check whether server is running on port 5432 or not...
if it is not running on it how to run on that port...

please tell me because I am a new bee to this postgresql... and I am trying this program from last 3 days...

Thanks & Regards...
Postgres on Linux or U* system?
If so write from pg_ctl status. If postgres isn't working start it using pg_ctl start command. If pg_ctl status doesn't work look into processes ps aux | grep post
Oct 12 '07 #4

P: 17
Postgres on Linux or U* system?
If so write from pg_ctl status. If postgres isn't working start it using pg_ctl start command. If pg_ctl status doesn't work look into processes ps aux | grep post
I tried 'pg_ctl' ... but it is not working...

so, I tried "ps aux | grep post" it is showing that postgresql server is running but I am not able to find whether it is working on the port 5432 or not...

The ouput of the "ps aux | grep post" :--

postgres 6589 0.0 0.0 20048 1152 pts/0 S 10:44 0:00 su postgres
postgres 6590 0.0 0.1 13684 4096 pts/0 S 10:44 0:00 bash
postgres 6729 0.0 0.1 44972 3860 pts/0 S 10:47 0:00 /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
postgres 6731 0.0 0.0 44972 1024 ? Ss 10:47 0:00 postgres: writer process
postgres 6732 0.0 0.0 15436 812 ? Ss 10:47 0:00 postgres: stats collector process
postgres 11065 0.0 0.0 8752 1120 pts/0 R+ 13:33 0:00 ps aux
postgres 11066 0.0 0.0 3944 880 pts/0 S+ 13:33 0:00 grep post


Thanks & Regards...
Oct 12 '07 #5

Expert 100+
P: 700
I tried 'pg_ctl' ... but it is not working...

so, I tried "ps aux | grep post" it is showing that postgresql server is running but I am not able to find whether it is working on the port 5432 or not...

The ouput of the "ps aux | grep post" :--

postgres 6589 0.0 0.0 20048 1152 pts/0 S 10:44 0:00 su postgres
postgres 6590 0.0 0.1 13684 4096 pts/0 S 10:44 0:00 bash
postgres 6729 0.0 0.1 44972 3860 pts/0 S 10:47 0:00 /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
postgres 6731 0.0 0.0 44972 1024 ? Ss 10:47 0:00 postgres: writer process
postgres 6732 0.0 0.0 15436 812 ? Ss 10:47 0:00 postgres: stats collector process
postgres 11065 0.0 0.0 8752 1120 pts/0 R+ 13:33 0:00 ps aux
postgres 11066 0.0 0.0 3944 880 pts/0 S+ 13:33 0:00 grep post


Thanks & Regards...
Can you connect from command line using psql?
See at /usr/local/pgsql/data/postgresql.conf and look for line begining with port string. See what port is postgres listening at.
Oct 12 '07 #6

P: 17
Can you connect from command line using psql?
See at /usr/local/pgsql/data/postgresql.conf and look for line begining with port string. See what port is postgres listening at.
Through command list I am able to connect with psql ... like psql test...

I saw the file... the port 5432 is there but it is in comment...


Thanks & Regards
Oct 12 '07 #7

Expert 100+
P: 700
Through command list I am able to connect with psql ... like psql test...

I saw the file... the port 5432 is there but it is in comment...


Thanks & Regards
Comment is ok, 5432 is default port for postgres

I'm not good at C-languge database programming, but maybe try to use option server with your ip or localhost into PQconnectdb function
Oct 12 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.