#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h>
#include <termios.h>
#include <time.h>
#include <stdbool.h>

#include <libpq-fe.h>

// gcc -o db_test.o -c -I/usr/include/postgresql db_test.c
// gcc -o db_test db_test.o -lpq

//-------------------------------------------------------------------------------------------------------------------------------//
int main(void)
{
 PGconn *pg_conn = NULL;
 PGresult *res = NULL;
 ConnStatusType status = CONNECTION_BAD;
 
 const char *conninfo = "host = 192.168.0.20 port = 20000 dbname = dbtest user = postgres password = postgres application_name = app-test";

  if((pg_conn = PQconnectdb(conninfo)) != NULL)
  {
   fprintf(stdout, "\n -CP00- \n");
	
   if(PQstatus(pg_conn) != CONNECTION_OK)
   {
	fprintf(stdout, "\n -CP01-[%d]- \n", PQstatus(pg_conn)); 
	  
    do
    {
	 sleep(2); 
     PQreset(pg_conn); 
	 status = PQstatus(pg_conn); 
	 fprintf(stdout, "\n -CP02-[%d]- \n", status);
    }
    while(status != CONNECTION_OK);
   }

   fprintf(stdout, "\n -CP03-[%d]- \n", PQstatus(pg_conn));
  
   res = PQexec(pg_conn, "SELECT 1 AS a");
	
   PQclear(res); res = NULL;

  
   
   PQfinish(pg_conn); pg_conn = NULL;
  }

	  
 return(0);
}
//-------------------------------------------------------------------------------------------------------------------------------//




 