diff --git a/connect/main.c b/connect/main.c new file mode 100644 index 0000000..e38e8f9 --- /dev/null +++ b/connect/main.c @@ -0,0 +1,60 @@ +#include +#include + +int pg_connect(char* conninfo, PGconn** conn) +{ + + *conn = PQconnectdb(conninfo); + if (PQstatus(*conn) != CONNECTION_OK) { + fprintf(stderr, + "ERROR: Connection to database failed: %s", + PQerrorMessage(*conn)); + return 0; + } + + return 1; +} + +/* set correctely your values here */ +#define PG_HOST "127.0.0.1" +//#define PG_USER "postgres" +//#define PG_DB "postgres" +//#define PG_PASS "postgres" +//#define PG_PORT 5432 + +int main(int argc, char *argv[]) +{ + /*declarations*/ + char conninfo[250]; + PGconn *conn = NULL; + + sprintf(conninfo, + "user=%s password=%s dbname=%s hostaddr=%s port=%d", + PG_USER, PG_PASS, PG_DB, PG_HOST, PG_PORT); + printf("main() conninfo: %s\n",conninfo); + + if (!pg_connect(conninfo, &conn)) { + printf("main() go to end\n"); + goto end; + }else{ + printf("main() go NOT to end\n"); + } + + /* + Here fit your staff + */ + int isThreadSafe=0; + printf("main() Started...\n"); + isThreadSafe=PQisthreadsafe(); + if(isThreadSafe==1){ + printf("main() libpq is thread-safe\n"); + }else{ + printf("main() libpq is NOT thread-safe\n"); + } + printf("main() Done.\n"); + + end: + PQfinish(conn); + return 0; + +} diff --git a/connect/makefile b/connect/makefile new file mode 100644 index 0000000..b14baca --- /dev/null +++ b/connect/makefile @@ -0,0 +1,24 @@ +# Docu +# +#$@ is the name of the file to be made +#$? is the names of the changed dependents +#$< the name of the related file that caused the action +#$* the prefix shared by target and dependent files +# +# Others +RM = /bin/rm -f +# +# Source, Executable, Includes, Library Defines +EXE = main +# +# Compiler, Linker Defines +CC = /usr/bin/gcc +# +all: main.o + $(CC) main.c -I/usr/include/postgresql -L/usr/lib/x86_64-linux-gnu -lpq -std=c99 -Wall -o $(EXE) +# +main.o: + $(CC) -c main.c -I/usr/include/postgresql +# Clean Up Objects, Exectuables, Dumps out of source directory +clean: + $(RM) *.o $(EXE) *~ diff --git a/connect/readme.md b/connect/readme.md new file mode 100644 index 0000000..75a5567 --- /dev/null +++ b/connect/readme.md @@ -0,0 +1,19 @@ +* install dependencies on Debian Bullseye +``` +sudo apt install libpq-dev --no-install-recommends +``` + +* build +``` +make +``` + +* run +``` +./main 'postgresql://:@:/' +``` + +* or run +``` +./main "host= port= user= password=<> dbname=" +```