How do you add PostgreSQL Driver as a dependency in Maven?
Asked Answered
A

5

86

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?

Adenocarcinoma answered 5/11, 2012 at 23:38 Comment(2)
PostgreSQL drivers jars are in Maven central repository: search.maven.org/…Gainey
FYI, 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.Micropathology
G
118

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)

Gainey answered 5/11, 2012 at 23:47 Comment(4)
Note 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 postgresqlIneffaceable
The 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, and your JDK 8 (JDBC 4.2) driver dependency is: <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.4-1206-jdbc42</version> </dependency>Deadhead
As of 2017-05, the numbering for the JDBC driver from jdbc.postgresql.org has been rebooted, currently 42.1.1.Micropathology
Even after adding the dependency, I see this error: #73381902Gmt
E
34

Updating for latest release:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.14</version>
</dependency>

Source

Hope it helps!

Electrodynamic answered 23/2, 2015 at 11:49 Comment(3)
I 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.Adenocarcinoma
It is, but you still have to look for the latest version. Isn't easier if you have everything in here?Electrodynamic
but the version is not latest anymore lolUmberto
I
17

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:

Intendment answered 5/11, 2012 at 23:43 Comment(0)
E
3

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"

Ensile answered 2/4, 2016 at 15:7 Comment(0)
I
0
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>
Idioglossia answered 19/5, 2018 at 19:50 Comment(1)
What version of postgresql it will expect it on runtime?Invocate

© 2022 - 2024 — McMap. All rights reserved.