Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Saturday, July 31, 2021

java.lang.ClassNotFoundException: org.postgresql.Driver - Cause and Solution

java.lang.ClassNotFoundException: org.postgresql.Driver error comes when you are trying to connect to a PostgreSQL database from Java program but Java ClassLoader is not able to find the Driver class "org.postgresql.Driver" required to make the connection. Usually, this class is found in PostgreSQL JDBC driver jars like postgresql-9.4-1201.jdbc41.jar, which is required to connect the PostgreSQL server version greater than 9.3 from JDK 1.7 or JDK 1.8, the exact JAR depends upon your PostgreSQL server version, the Java version you are running, and JDBC version you are using.

Now your problem could be either you don't have that PostgreSQL JDBC driver JAR in your machine or the JAR is not in your classpath, or you might be battling with some classpath intricacies.

If you don't have this JAR, then a solution of "java.lang.ClassNotFoundException: org.postgresql.Driver" is simple, just download it from the PostgreSQL site. Make sure you download the correct version of the JDBC driver based upon the PostgreSQL server you are connecting and the JVM version of your machine.

Once you download that, just put in the lib directory of your Java application e.g. WEB-INF/lib if you are connecting to PostgreSQL from Java Web Application. If you are running in Tomcat, then you can alternatively also put this in tomcat/lib directory, but beware of the difference in the application dependency in WEB-INF/lib or in tomcat/lib.

You can also take a look at these JDBC Online Courses on Udemy. This course is a comprehensive guide on how to use JDBC in Java to connect to different databases. You will learn the right ways of doing things with respect to Java and the database.

Cause of java.lang.ClassNotFoundException: org.postgresql.Driver

In order to connect to the PostgreSQL database from Java, the first step is to register the driver with DriverManager. Generally, the Class.forName() method is used to find, load, and register JDBC driver. So if your application executes Class.forName("org.postgresql.Driver"), then Java's class loader tries to find this class in all the JAR files included in CLASSPATH.

If they are able to find then they load, instantiate and register the driver otherwise it throws java.lang.ClassNotFoundException: org.postgresql.Driver. Many times, what will happen that the JAR file will be there but you will still be getting java.lang.ClassNotFoundException: org.postgresql.Driver because of classpath mysteries, this is why I suggest you read my post, how ClassPath works in Java.

Sometimes, you add this JAR into the CLASSPATH environment variable, but your application would be running using -cp or -classpath option, so it will ignore the CLASSPATH environment variable. If that's the case then you must include it in the -cp or -classpath option.

Some other times, like in the case of Servlet JSP based application, not putting the   JAR postgresql-9.4-1201.jdbc41.jar in WEB-INF/lib folder. This error is also exactly similar to java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver, which comes when you try to connect to Oracle database, and driver's JAR is not in CLASSPATH. The steps and approaches mentioned there also apply to solve this problem.

Solution of java.lang.ClassNotFoundException: org.postgresql.Driver

Depending upon your setup, the solution could be any of these :

1) You need a PostgreSQL JDBC driver to connect from a Java program e.g. postgresql-9.4-1201.jdbc41.jar. Actual JDBC JAR could be different, depending upon the PostgreSQL server you are connecting and the Java version you are running. You can also download PostgreSQL driver from

2) If you already have this JAR and your Java program is not running with -cp or -classpath option, then edit the CLASSPATH environment variable and add the directory, where you have put this JAR file. You can edit CLASSPATH in windows as set CLASSPATH = %CLASSPATH%; (location of PostgreSQL JDBC driver) and export CLASSPATH = ${CLASSPATH};postgresql-9.2-1002.jdbc3.jar

3) If your Java program is running with -cp or -classpath option then add the location of postgresql-9.2-1002.jdbc3.jar (PostgreSQL version 9.2 for Java version 1.6 or lesser with JDBC 3.0) as well e.g.
java -cp .;postgresql-9.2-1002.jdbc3.jar JavaToPostgreSQL, where JavaToPostgreSQL is name of your Java program. Remember separator in Windows is ; (Semi colon ) and separator in Linux is : (colon)

4) If you are connecting PostgreSQL db from Java Web application then put postgresql-9.4-1201.jdbc41.jar into your WEB-INF/lib folder, if your tomcat is running on Java 1.7 or 1.8 version.

5) If you are using a java version older than 1.6 then you will need to use a JDBC3 version of the driver i.e. postgresql-9.3-1103.jdbc3.jar. You can find the right version of the JDBC driver from the PostgreSQL site, depending upon the factors e.g. PostgreSQL version or Java version.

How to fix java.lang.ClassNotFoundException: org.postgresql.Driver in Java

That's all about how to fix java.lang.ClassNotFoundException: org.postgresql.Driver error in Java. As I said, this error is very straightforward to solve, you just need the PostgreSQL JDBC driver in your CLASSPATH. The difficult thing is to understand how Classpath works, so make sure you read that tutorial.

A couple of other common errors and exceptions comes while connecting to other popular databases from Java programs like MySQL, Oracle, and Microsoft SQL Server
  • How to connect to MySQL database from Java Program [steps]
  • General Guide to solve java.lang.ClassNotFoundException in Java [guide]
  • How to solve java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver in Java? [solution]
  • How to fix java.lang.ClassNotFoundException: org.postgresql.Driver error in Java? [solution]
  • How to solve java.lang.ClassNotFoundException:org.Springframework.Web.Context.ContextLoaderListener [solution]
  • How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Java MySQL? [solution]
  • java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory? [solution]
  • How to fix java.lang.ClassNotFoundException: in Java? [solution]
P. S. - If you already have the postgresql-9.2-1002.jdbc3.jar (or any other PostgreSQL JDBC driver) in classpath but still getting this error then please post in a comment what did you try and your setup e.g. CLASSPATH and we will try to troubleshoot together. In almost all cases its the CLASSPATH intricacies which cause "java.lang.ClassNotFoundException: org.postgresql.Driver".


Petrus Kambala said...

Thank you. This fixed my problem.

Digao said...

this fixed my problem too. Thanks a lot !!

BanAnanas said...

In my case I was missing

runtime 'org.postgresql:postgresql:42.2.1'

in build.gradle file, I only had

compile 'postgresql:postgresql-connector-java'
So you might want to check that out before following this steps, if you are faced with the error

javin paul said...

Thanks, it will definitely help others.

Post a Comment