Problem with PostgreSQL / Clojure drivers

I am trying to access a Postgres database inside Clojure. I found a ton of sample projects using the database, creating the database as follows:

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

Then I try to access the database as follows:

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

However, I get this error:

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

I assume the problem is related to :classname "org.postgresql.Driver" , but I'm not sure what the solution is. I suppose 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 it? Or is there something I can contribute to my project settings to get lein to load it as a dependency? As soon as I succeed, where will he go?


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

 (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 
+8
java postgresql clojure jdbc
source share
1 answer

You are on the right track. An exception indicates that your classpath does not have org.postgresql.Driver anywhere.

By checking jarvana.com, I find this entry for the postgres JDBC 4 driver. Other versions are available, depending on the rest of the runtime. You can enable this by editing the project.clj file to add this dependency:

 (defproject xxxxxxx ;;; other stuff :dependencies [[org.clojure/clojure "1.2.0"] [postgresql/postgresql "9.0-801.jdbc4"]] ) 
+8
source share

All Articles