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

153,827

Solution 1

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)

Solution 2

Updating for latest release:

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

Source

Hope it helps!

Solution 3

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

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

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>
Share:
153,827

Related videos on Youtube

Sotirios Delimanolis
Author by

Sotirios Delimanolis

Couple posts on Medium: When you think you found a bug in TCP, don’t get cocky, kid. The other side of Stack Overflow content moderation

Updated on July 08, 2022

Comments

  • Sotirios Delimanolis
    Sotirios Delimanolis almost 2 years

    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?

  • Caspar
    Caspar over 10 years
    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 postgresql
  • Sotirios Delimanolis
    Sotirios Delimanolis about 9 years
    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.
  • facundofarias
    facundofarias over 8 years
    It is, but you still have to look for the latest version. Isn't easier if you have everything in here?
  • Jan Nielsen
    Jan Nielsen over 8 years
    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>
  • Basil Bourque
    Basil Bourque about 7 years
    As of 2017-05, the numbering for the JDBC driver from jdbc.postgresql.org has been rebooted, currently 42.1.1.
  • Pathik Mehta
    Pathik Mehta over 3 years
    What version of postgresql it will expect it on runtime?
  • vanjavk
    vanjavk about 3 years
    but the version is not latest anymore lol