This article provides instructions for adding the PostgreSQL JDBC driver to a Java project using Maven.
- Introduction
- Why do we need PostgreSQL drivers?
- What is Maven?
- PostgreSQL JDBC driver and Maven repositories
- What does a POM file do?
- Step-by-step instructions for adding the PostgreSQL JDBC driver dependency
Have you at any point needed to connect to your PostgreSQL database using Java and didn't have a clue how to add it as a Maven dependency?
Now you can use this guide as an overview.
Introduction
Before jumping in to the nitty gritty, let's first get a basic overview on why we need a PostgreSQL driver.
Why do we need PostgreSQL drivers?
To connect a frontend application to any database you need to use a respective database driver. These drivers vary for different frontend programming languages. Once primarily available for relational databases, they are now available for almost any type of data source, such as Big Data, NoSQL, and SaaS. One such driver is JDBC driver. JDBC drivers are the easiest way for many developers to connect to databases from their Java applications.
Drivers do a fair amount of work, from the basics of opening socket connections from your Java application to the database and submitting your SQL queries, to more advanced features, like offering the ability to receive events from the database.
To connect your database in Java, you need to have a JDBC driver. PostgreSQL, the world's best open source relational database, comes with its own JDBC driver, which can be found on the PostgreSQL community website. So, to connect to a PostgreSQL database, you will need to go to the PostgreSQL website, download the PostgreSQL JDBC driver JAR file, and add it to your project.
What is Maven?
Maven is a build automation tool used primarily for Java projects. Maven can also be used to build and manage projects written in C#, Ruby, Scala, and other languages. The Maven project is hosted by the Apache Software Foundation, where it was formerly part of the Jakarta Project. It streamlines the project development process, enabling you to create and publish swiftly.
PostgreSQL JDBC driver and Maven repositories
There are different approaches to design and configure your project in Java. One of the most common is using Apache Maven. The benefit of Apache Maven is that you only need to add dependencies in the POM file; the rest is taken care of by Maven. You do not need to add the JAR files manually yourself. You can simply add the PostgreSQL JDBC dependency to the project's POM file. You can browse around the Maven repository to get a sense of the many dependencies that might be needed for your project.
What does a POM file do?
POM stands for Project Object Model. It is an XML file that stores crucial information about a Maven project, including configuration details used by Maven. It's the place where we can specify plugins, dependencies, and project versions.
Below is a sample POM file where we can see a few basic XML tags.
Maven uses these tags to uniquely identify the project. Here, <groupId> is the name of the company or group that created this project, <artifactId> is the name of the project, <version> as the name suggest is the version of the project, <dependencies> is the main tag where we will include all the dependencies of the project, and <dependency> is the tag where we specify individual dependencies.
The ZIP format Java ARchive (JAR) file is essential in Maven project development. JAR files help you compress, decompress, and archive files. You also need libraries when you construct projects. In Maven, libraries, JAR files, are all collectively called dependencies because you add them to the pom.xml file to create your Maven project.
Step-by-step instructions for adding the PostgreSQL JDBC driver dependency
Here are step-by-step instructions for how to add PostgreSQL’s JDBC driver dependency. As a prerequisite, I will assume you have any JAVA IDE installed on your system, like Eclipse, Netbeans, etc.
We will be using Netbeans for a sample test project.
1. Open Netbeans and Click on File → New Project.
2. A pop up will open asking you to enter Categories and Project type, As we are using Maven I have chosen Maven as Category and POM as project type, as shown below:
3. Next, it will ask you to enter project name and other details:
4. After clicking Finish, you will notice a tree on the left-hand side of your screen where Modules, Project Files, and Dependencies are listed.
5. As this is a new project, you will notice that the Dependencies tree is empty:
6. Here you will also find your project main POM file, which will look like this:
7. This is also the place where we can add the dependencies. You can check this Maven Central link to find out which is the latest version of the PostgreSQL JDBC artifact.
8. At the time of this writing, 42.2.15 is the latest PostgreSQL JDBC Driver version available, so we will be using that in our test project. On Maven Central you will be able to find the tag for the dependency, as shown below:
9. Copy the above dependency tag into your project pom.xml, as shown below:
10. As soon as you save your POM file, you will notice that the Dependencies tree is updated, and PostgreSQL JDBC is displayed.
I hope that this article has proven helpful in giving a sense of how easy it is to add PostgreSQL JDBC driver as a dependency in Maven.