I'm trying to develop a Java application with Maven while using Hibernate with a PostgreSQL database for persistence. I don't understand how I'm supposed to connect the PostgreSQL drivers to my application. I get that you add dependencies in Maven's pom.xml file, which finds jars from a remote repository, but what about other jars?
-
4PostgreSQL drivers jars are in Maven central repository: http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22postgresql%22%20AND%20a%3A%22postgresql%22 – madth3 Nov 05 '12 at 23:41
-
1FYI, for a twist on this Question when working in a web app where the JDBC driver needs to be deployed in your web container rather than in your WAR file, see my Question: [Include a library while programming & compiling, but exclude from build, in NetBeans Maven-based project](https://stackoverflow.com/q/32087445/642706). – Basil Bourque Jan 22 '18 at 01:25
5 Answers
PostgreSQL drivers jars are included in Central Repository of Maven:
For PostgreSQL up to 9.1, use:
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>VERSION</version>
</dependency>
or for 9.2+
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>VERSION</version>
</dependency>
(Thanks to @Caspar for the correction)

- 7,275
- 12
- 50
- 74
-
9Note that for 9.2 and later, the groupid for the jdbc driver has changed so you need to [search for a groupid of org.postgresql instead of postgresql](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.postgresql%22%20AND%20a%3A%22postgresql%22) – Caspar Oct 01 '13 at 01:39
-
6The latest PostgreSQL JDBC driver supports PostgreSQL Server 7.2+; so, no need for the first (pre-9.2) block. You can find [the latest JDBC drivers from the PostgreSQL site](https://jdbc.postgresql.org/download.html), and your JDK 8 (JDBC 4.2) driver dependency is: ```
org.postgresql postgresql 9.4-1206-jdbc42 -
3As of 2017-05, the numbering for the [JDBC driver from jdbc.postgresql.org](https://jdbc.postgresql.org/download.html) has been rebooted, currently 42.1.1. – Basil Bourque May 17 '17 at 06:19
-
Even after adding the dependency, I see this error: https://stackoverflow.com/questions/73381402/java-lang-classnotfoundexception-org-postgresql-driver-in-terminal – ASR4 Aug 17 '22 at 04:14
Updating for latest release:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.14</version>
</dependency>
Hope it helps!

- 2,973
- 28
- 27
-
10I don't think this answer is necessary. Consider contributing an edit to the accepted answer if you don't think its `VERSION` is indicative enough to get the latest version. – Sotirios Delimanolis Feb 23 '15 at 15:33
-
It is, but you still have to look for the latest version. Isn't easier if you have everything in here? – facundofarias Sep 01 '15 at 16:26
-
1
Depending on your PostgreSQL version you would need to add the postgresql driver to your pom.xml
file.
For PostgreSQL 9.1 this would be:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<name>Your project name.</name>
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
</dependencies>
</project>
You can get the code for the dependency (as well as any other dependency) from maven's central repository
If you are using postgresql 9.2+:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<name>Your project name.</name>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.1</version>
</dependency>
</dependencies>
</project>
You can check the latest versions and dependency snippets from:

- 16,203
- 11
- 62
- 106
From site PostgreSQL, of date 02/04/2016 (https://jdbc.postgresql.org/download.html):
"This is the current version of the driver. Unless you have unusual requirements (running old applications or JVMs), this is the driver you should be using. It supports Postgresql 7.2 or newer and requires a 1.6 or newer JVM. It contains support for SSL and the javax.sql package. If you are using the 1.6 then you should use the JDBC4 version. If you are using 1.7 then you should use the JDBC41 version. If you are using 1.8 then you should use the JDBC42 versionIf you are using a java version older than 1.6 then you will need to use a JDBC3 version of the driver, which will by necessity not be current"

- 1,182
- 1
- 15
- 23

- 41
- 3
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>

- 29
- 2