473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

PostgresSQL server connectiong problem with C-language...

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

The program is as fallows:-
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "libpq-fe.h"                                   /* libpq header file */
  4. int
  5. main()
  6. {
  7.     char        state_code[3];                         
  8.     char        query_string[256];                      /* holds constructed SQL query */
  9.     PGconn     *conn;                                   /* holds database connection */
  10.     PGresult   *res;                                    
  11.     int         i;
  12.     conn = PQconnectdb("dbname=test");                  /* connect to the database */
  13.     if (PQstatus(conn) == CONNECTION_BAD)               /* did the database connection fail? */
  14.     {
  15.         fprintf(stderr, "Connection to database failed.\n");
  16.         fprintf(stderr, "%s", PQerrorMessage(conn));
  17.         exit(1);
  18.     }
  19.     printf("Enter a state code: ");                     /* prompt user for a state code */
  20.     scanf("%2s", state_code);
  21.     sprintf(query_string,                               /* create an SQL query string */
  22.             "SELECT name \
  23.              FROM statename \
  24.              WHERE code = ’%s’", state_code);
  25.     res = PQexec(conn, query_string);                   /* send the query */
  26.     if (PQresultStatus(res) != PGRES_TUPLES_OK)         /* did the query fail? */
  27.     {
  28.         fprintf(stderr, "SELECT query failed.\n");
  29.         PQclear(res);
  30.         PQfinish(conn);
  31.         exit(1);
  32.     }
  33.     for (i = 0; i < PQntuples(res); i++)                /* loop through all rows returned */
  34.         printf("%s\n", PQgetvalue(res, i, 0));          /* print the value returned */
  35.     PQclear(res);                                       /* free result */
  36.     PQfinish(conn);                                     /* disconnect from the database */
  37.     return 0;
  38. }
  39.  
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
3 1371
weaknessforcats
9,208 Expert Mod 8TB
Please use code tags.

I added them for you this time.
Oct 12 '07 #2
Please use code tags.

I added them for you this time.

I tried with ur code ... but I am still getting the same error...

even I changed the posgresql.conf and pg_hda.conf. and tried same result.

Thanks & Regards...
Oct 13 '07 #3
Meetee
931 Expert Mod 512MB
I tried with ur code ... but I am still getting the same error...

even I changed the posgresql.conf and pg_hda.conf. and tried same result.

Thanks & Regards...
Moderator has not given new code. He has just put code tags which will make your code and post clearly readable. In Reply Guidelines, it is mentined that when you post any code "Use CODE tags around your code:[ CODE ]..code goes here..[ /CODE ]" So your code is not changed anyway by him :)

Regards
Oct 13 '07 #4

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

Similar topics

0
by: Fredrik Möller | last post by:
Hi all, I need help in solving a connection problem. There is a failover cluster running SQL Server 2000 in to instances, 'AUTUMN-03' and 'WINTER-03\I1'. On an other machine I run a service,...
0
by: Marco | last post by:
I need to know if is possible to read oracle tables from PostgresSQL (and vice versa). Can someone give me some help? Thanks a lot
3
by: Serdar C. | last post by:
how can i connect to a smtp server and check my email account if i have e mails and maybe looking at their subject, etc,.. couldnt fnd a place for smtp commands and cnonecting to a smtp server??
2
by: Dino Buljubasic | last post by:
Hi, My application is extensivelly querying a remote server. Somebody sniffing the traffic could eventually find out all important information such as passwords (from users in datatables as...
1
by: Davey | last post by:
I have Visio for Visual Studio.NET Enterprise Architect 2003 and I am designing a PostgresSQL database using it. Is it possible to generate and manage a PostgresSQL database using Visio?
10
by: MVChauhan | last post by:
Hi We are planning to move over to SQL Server 2005 in near future. At the moment Website is on a seperate server then the Database. OS for both the server is Window 2003 and currently our data...
5
by: varathasiva | last post by:
Dear All, I have instaled PostgresSQL 8.2 in windows xp system.That windows xp system have a static ip.I already update that static ip in pg_hba.config files after stop start postgresql...
1
dmjpro
by: dmjpro | last post by:
Is there any tool or any alternative way to import database objects from ORACLE 9i+ to postgresSQL? Plz help. Kind regards, Dmjpro.
5
by: =?Utf-8?B?RGFuaWVs?= | last post by:
How can I switch connection strings stored in config file based on the build configuration? Or my real question is how most people maintain different connection strings when their code is running...
0
by: jaleel | last post by:
Hi All, I have a dotnet application (Windows appln) from which I am connecting to SQL server 2005. My doubt is can I connect to SQL server DB in a remote machine from the machine where the Dotnet...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.