From dd8323939e91b262e0b24a583a9ceef4424a08d4 Mon Sep 17 00:00:00 2001 From: Joachim Desroches Date: Mon, 27 Sep 2021 15:35:20 +0200 Subject: [PATCH] Connect to PSQL database. --- exporter.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/exporter.c b/exporter.c index 14aa5a1..4615bbd 100644 --- a/exporter.c +++ b/exporter.c @@ -12,5 +12,21 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } + /* Setup database connection parameters */ + char connstring[STRLEN]; + sprintf(connstring, "database = %s, user = %s", argv[1], USER); + + /* Connect to the database */ + PGconn *conn = PQconnectdb(connstring); + + if (conn == NULL) { + fprintf(stderr, "Out of memory connecting the PostgreSQL\n."); + exit(EXIT_FAILURE); + } else if (PQstatus(conn) != CONNECTION_OK) { + fprintf(stderr, PQerrorMessage(conn)); + PQfinish(conn); + exit(EXIT_FAILURE); + } + return EXIT_SUCCESS; }