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

JavaHibernatePostgresqlMaven

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

PostgreSQL drivers jars are included in Central Repository of Maven:

  • [List of included versions of PostgreSQL drivers.][1]

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) [1]: http://search.maven.org/#search%7Cga%7C1%7C(g%3A%22org.postgresql%22%20OR%20g%3A%22postgresql%22)%20AND%20a%3A%22postgresql%22

Solution 2 - Java

Updating for latest release:

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

Source

Hope it helps!

Solution 3 - Java

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:

Solution 4 - Java

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"

Solution 5 - Java

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSotirios DelimanolisView Question on Stackoverflow
Solution 1 - Javamadth3View Answer on Stackoverflow
Solution 2 - JavafacundofariasView Answer on Stackoverflow
Solution 3 - JavatftdView Answer on Stackoverflow
Solution 4 - JavaDaniel SavaView Answer on Stackoverflow
Solution 5 - JavaByteCode ThreadView Answer on Stackoverflow