Hello, I'm having problems interacting with a Postgres DB with Nim
proc handleDbSync(dbParams: tuple[dbAddr: string, user: string, password: string, db: string]) {.thread.} =
var logger = newConsoleLogger()
var db: DbConn
logger.log(lvlInfo, "DB: " & dbParams.user & ":" & dbParams.password & "@" & dbParams.dbAddr & "/" & dbParams.db)
try:
let db = db_postgres.open(dbParams.dbAddr, dbParams.user, dbParams.password, dbParams.db)
defer: db.close()
except DbError:
logger.log(lvlError, "Failed to connect to Postgres DB!")
return
let query = db.prepare("query_name", sql"""
INSERT INTO tt_records (id,
device_uid,
inserted_on,
task_start,
task_end,
project_id,
client_ip,
comment
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
""", 8)
if true == db.tryExec(query,
0, "fafafafa",
"1999-01-08 04:05:06 -8:00",
"1999-01-08 04:05:06 -8:00",
"1999-01-08 04:05:06 -8:00",
12, "192.168.6.9",
"Komentarz taki taki"
):
logger.log(lvlInfo, "Inserted into DB!")
The code above results in:
INFO DB: user:pswd@localhost/db
/Users/michal/gits/time_server/server_nim/server.nim(207) handleDbSync
/usr/local/Cellar/nim/1.4.8/nim/lib/impure/db_postgres.nim(194) prepare
/usr/local/Cellar/nim/1.4.8/nim/lib/impure/db_postgres.nim(107) dbError
Error: unhandled exception: connection pointer is NULL
[DbError]
It's not reporting the "Failed to connect to Postgres DB!" but it seems it fails to properly connect. The Postgres DB is running on the same machine in a Docker container (default port). I can connect to the DB with a cli tools.
CLI tools from within the container or from within the host machine? I'm guessing you have the port forwarding set up for your docker image.
A couple of things about your code...
First of all, you're declaring var db: DbConn and then later you're assigning a value let db =
You don't need to forward declare the db variable. In fact what you're doing here is declaring a mutable instance of DbConn and then shadowing it by declaring another instance of a immutable DbConn, which isn't want you want. Get rid of the var db: DbConn
Next problem is your error handling logic and your call to defer. You're getting a pointer back from the postgres client connection code, and potentially a DbError. Your except block is fine, but you're not checking to see if the pointer to the client connection is nil before calling defer on it. That's probably the source of your error.
As to why you can't connect - I'm guessing it's either because your networking is screwed up with your docker container or you have your connection string wrong. Make sure you read the docs on the postgresql https://nim-lang.org/docs/db_postgres.html - might help you figure out what exactly is wrong in your code.
Oh, you're correct about the two instance of db. I can't get rid of the var db:DbConn because of the scoping. let db is valid only in the try block. I've removed the let. That also made me realize that defer is called at the end of the try block... I've also removed defer: db.close() and with those two changes everything works.
I was running the cli tool on the host machine, not in the docker container. Everything is fine with the docker setup.
Thank you so much for you help :) It was a bit of a silly mistake on my part.