8

I am attempting to access a Postgres database inside of Clojure. I've found a ton of examples of projects using DBs, setting up the database like this:

(def db
    {:classname "org.postgresql.Driver"
     :subprotocol "postgresql"
     :subname "//localhost/testdb"
     :username "postgres"
     :password "postgres"})

I'm then trying to then access the database like this:

(sql/with-connection db
    (sql/with-query-results recs ["select * from asdf"]
        (doseq [rec recs]
            (println rec))))

However, I'm getting this error:

No suitable driver found for jdbc:postgresql://localhost/testdb
  [Thrown class java.sql.SQLException]

I'm assuming the problem is with :classname "org.postgresql.Driver", but I'm not sure what the solution is. I imagine I need to provide this driver, but I'm not sure where to get it or where to put it. There is a download available at postgresql.org - should I download that? Or is there something I can put in my project settings to get lein to download it as a dependency? Once I have it, where does it go?


Edit (in response to @mtnygard): I have this in my project.clj:

(defproject hello-www "1.0.0-SNAPSHOT"
    :dependencies [[org.clojure/clojure "1.2.1"]
                   [postgresql/postgresql "8.4-702.jdbc4"]
                   ...]

My postgres version is 8.4:

[/media/data/dev/clojure/hello-www (postgres *)]$ postgres --version
postgres (PostgreSQL) 8.4.8
3
  • Did you run "lein deps" after adding postgresql to your project.clj? Commented May 25, 2011 at 4:27
  • Also, how are you executing the main body of your project? "lein deps" just puts the jars in lib/. You still have to run your program with a configured classpath. "lein run" and "lein repl" will both set up your classpath, for example. Commented May 25, 2011 at 4:29
  • You found the problem! I had run lein deps, but I had not updated my REPL. Restarting swank gave me a connection. Thanks! Commented May 25, 2011 at 4:32

1 Answer 1

8

You are on the right track. The exception indicates that your classpath doesn't have org.postgresql.Driver anywhere.

Checking jarvana.com, I find this entry for a postgres JDBC 4 driver. There are other versions available, depending on the rest of your runtime environment. You can include this by editing your project.clj file to add this dependency:

(defproject xxxxxxx
  ;;; other stuff

  :dependencies [[org.clojure/clojure "1.2.0"]
                 [postgresql/postgresql "9.0-801.jdbc4"]]
 )
Sign up to request clarification or add additional context in comments.

2 Comments

I already have postgresql in my project.clj - please see my updated answer.
For anyone with the same problem- make sure the above is in project.clj. Then, run lein deps. You may have to restart your REPL if you're in one to make sure it gets the update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.