I'm a complete novice trying to learn PostgreSQL. I'm trying to connect to my postgres server throught a C program using libpq.
Here is the server status:
home/planb/postgresql-9.2.4/Project status -o "-p 5555"
pg_ctl: server is running (PID: 2338)
/usr/local/pgsql/bin/postgres "-D" "/home/planb/postgresql-9.2.4/Project" "-p5555"
When I compile, I use:
gcc -I /usr/local/pgsql/include -L /usr/local/pgsql/lib test.c -lpq
When I run the program with ./a.out, it reads:
Connection error
I believe I'm not using PQconnectdb correctly, but it could be other things.
Here is my C file: test.c
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
int main(int argc, char* argv[])
{
//Start connection
PGconn* connection = PQconnectdb("hostaddr=168.105.127.3 port=5555 dbname=Project username=postgres password=password");
if (PQstatus(connection) ==CONNECTION_BAD)
{
printf("Connection error\n");
PQfinish(connection);
return -1; //Execution of the program will stop here
}
printf("Connection ok\n");
//End connection
PQfinish(connection);
printf("Disconnected\n");
return 0;
}
Any input is greatly appreciated, thanks!
Figured it out!
I wasn't using a valid hostaddr. I replaced it with:
host=localhost
I also deleted dbname=Project. When I run it, I get:
Msg: Connection ok
Disconnected