diff options
author | Michael McVady <femtonaut@gmail.com> | 2021-09-23 00:22:10 -0500 |
---|---|---|
committer | Michael McVady <femtonaut@gmail.com> | 2021-09-23 00:22:10 -0500 |
commit | 40441e01b6e73a0a1d7f06377b3607f151aed65b (patch) | |
tree | f78149c19b55af7dcaec3d8de165c4c8b9186fab | |
parent | 3595c5264297f14010eb8dc6f0a7fd31dc186e8b (diff) |
Add pgsql sandbox
-rw-r--r-- | db/db.sql | 35 | ||||
-rw-r--r-- | sandbox/pgsql/Makefile | 5 | ||||
-rw-r--r-- | sandbox/pgsql/build | 3 | ||||
-rw-r--r-- | sandbox/pgsql/pgsql.c | 28 |
4 files changed, 71 insertions, 0 deletions
diff --git a/db/db.sql b/db/db.sql new file mode 100644 index 0000000..3f27856 --- /dev/null +++ b/db/db.sql @@ -0,0 +1,35 @@ +CREATE DATABASE clog; + +\connect clog + +CREATE TABLE entries ( + id BIGINT NOT NULL, -- PRIMARY KEY UUID + title CHARACTER VARYING NOT NULL, + body CHARACTER VARYING NOT NULL + -- created_at TIMESTAMP NOT NULL, + -- updated_at TIMESTAMP NOT NULL +); + + +INSERT INTO entries ( + id, + title, + body +) +VALUES +( + 1, + 'foo', + 'bar' +), +( + 2, + 'baz', + 'zoo' +), +( + 3, + 'xxx', + 'yyy' +); + diff --git a/sandbox/pgsql/Makefile b/sandbox/pgsql/Makefile new file mode 100644 index 0000000..1037bc4 --- /dev/null +++ b/sandbox/pgsql/Makefile @@ -0,0 +1,5 @@ +build: + gcc pgsql.c -I/usr/include/postgresql -lpq -o pgsql + +clean: + rm pgsql diff --git a/sandbox/pgsql/build b/sandbox/pgsql/build new file mode 100644 index 0000000..f5e320e --- /dev/null +++ b/sandbox/pgsql/build @@ -0,0 +1,3 @@ +gcc -c -I/usr/include/postgresql pgsql.c -L/usr/include/pgsql -lpq -o pgsql +pg_config --libdir +pg_config --includedir diff --git a/sandbox/pgsql/pgsql.c b/sandbox/pgsql/pgsql.c new file mode 100644 index 0000000..ffbae9f --- /dev/null +++ b/sandbox/pgsql/pgsql.c @@ -0,0 +1,28 @@ +#include <stdio.h> + +#include <libpq-fe.h> +#include <pg_config.h> + +int main() { + PGconn* conn; + PGresult* result; + + conn = PQconnectdb("host=localhost port=5432 dbname=clog password=p0stgres user=postgres"); + + if(PQstatus(conn) != CONNECTION_OK) { + printf("Connection to database failed:%s\n", PQerrorMessage(conn)); + } + + result = PQexec(conn, "SELECT * FROM ENTRIES"); + + PQprintOpt options = {0}; + options.header = 1; /* Ask for column headers */ + options.align = 1; /* Pad short columns for alignment */ + options.fieldSep = "|"; /* Use a pipe as the field separator */ + + PQprint(stdout, result, &options); + + PQclear(result); + PQfinish(conn); +} + |