Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Driver issue with PostgreSQL/Clojure

Tags:

People also ask

Is PostgreSQL JDBC compliant?

It provides a standard set of interfaces to SQL -compliant databases. PostgreSQL provides a type 4 JDBC driver.

What is PostgreSQL JDBC driver?

PostgreSQL JDBC Driver (PgJDBC for short) allows Java programs to connect to a PostgreSQL database using standard, database independent Java code. Is an open source JDBC driver written in Pure Java (Type 4), and communicates in the PostgreSQL native network protocol.


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