Search code examples
postgresqljdbcclojureleiningen

Why Can't JVM Find Postgresql JDBC Driver?


I know this question has been asked before, but existing answers do not help.

Existing answers tend to either tell you to put postgresql-.jar in /path/to/db/lib/, which I did; or center around MySQL. I've tried > 5 different answers and nothing is helping.

Here's what I'm running at the Clojure REPL:

(require '[next.jdbc :as jdbc])
(def db {:dbtype "postgres" :dbname "whatever" :user "whoever" :password "yeahright"})
(jdbc/get-connection db)

And I get this error:

Execution error (SQLException) at java.sql.DriverManager/getConnection (DriverManager.java:702).
 No suitable driver found for jdbc:postgresql://127.0.0.1:5432/<db_name>

I know where postgresql-42.2.12.jar is located on my computer, and I used :classpath-add to add that path in project.clj. I have double checked the syntax of the URL and it's correct so far as I can tell.

Java version:

  $ java -version
  java version "10.0.1" 2018-04-17
  Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
  Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)

  $ lein -version
  Leiningen 2.9.1 on Java 10.0.1 Java HotSpot(TM) 64-Bit Server VM

I have the same problem on Ubuntu & Mac. Does anyone know what is going on?


Solution

  • Per the next.jdbc Getting Started guide https://cljdoc.org/d/seancorfield/next.jdbc/CURRENT/doc/getting-started

    "In addition, you will need to add dependencies for the JDBC drivers you wish to use for whatever databases you are using."

    For lein that means adding the following to your :dependencies vector in project.clj:

    [org.postgresql/postgresql "42.2.10"]
    

    (that's the latest version that next.jdbc is tested against at the time of writing, May 23rd, 2020)