Connecting to the Database


#1

Originally published at: https://purelyfunctional.tv/complete-web-app-from-scratch/connecting-to-the-database/
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.


#2

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?


#3

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: https://github.com/clojure/java.jdbc#developer-information

I hope that helps.

Eric


#4

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!


#5

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)