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 ca开发者_开发百科n 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
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"]]
)
精彩评论