Connecting to the Database


Originally published at:
We extend our very simple web server by connecting it to a PostgreSQL database. We use HugSQL to express our SQL and connect. We begin by creating our first table.


I think (install-uuid-module db) worked in your video because you already ran it from the super user role before or your connecting to your db as super user.

On my machine I had to run it in the console as the super user. If I then run in it my Clojure project, it responds the same as yours in the video.

One question that leads from this, is:
How can I perform a createdb and dropdb and other super user actions from within Clojure?


Hi @thinkadoo,

That’s a great question. I have postgres set up on my local machine to accept all connections as super user. It’s handy for development.

But the same kind of thing works on Heroku. Could it be a setting on your local postgres?

I did run createdb as a superuser (my regular login is superuser). But I didn’t create the postgres model beforehand. I’m not sure how to run createdb from Clojure.

This is how they create databases for testing:

I hope that helps.



Thank you Eric,

That did help me.

I figured out that its possible to connect without a username or password in the connection string
Initially I created a user for the project and signed in with that user (role)
(def db “jdbc:postgresql://localhost/clojureweb?user=hypster&password=password”)
Now I am connecting with the super user (role) by not defining name and password.
(def db “jdbc:postgresql://localhost/clojureweb”)

That works!


The checkpoint [git checkout 2]

Has a call to an undefined function (init-db)
Just comment it out if you see an error.

L19 ;;(init-db)