Summary: in this tutorial, we will show you how to setup Java environment, download PostgreSQL JDBC driver, and connect to the PostgreSQL database server from a Java program.
Setting up Java development environment
To develop a Java program, you need to have JDK installed on your computer. To setup JDK, first, you go to the Oracle website to download the latest JDK. Then, you install it on your computer.

The setup is straightforward, you just need to accept the default parameters provided the installer and you are set.
Practically, to develop java applications, you need to have a good IDE. There are many good IDE available for free such as Eclipse, NetBeans, IntelliJ IDEA Community Edition, etc.
In this tutorial, we will use the NetBeans IDE to develop Java application. You can download the NetBeans IDE directly from the Oracle website or you can download it from the NetBeans download page.

You can follow the steps as shown in the animated picture below to install the NetBeans IDE.
Download PostgreSQL JDBC Driver
To connect to the PostgreSQL database server from a Java program, you need to have PostgreSQL JDBC driver. You can download the latest version of the driver on the postgresql.org website via the download page.
The downloaded file is a jar file. You should copy it to a specific folder e.g. C:\demo\libs so that you can remember its location and be able to add it to your Java application later.
Connect to the PostgreSQL database server
First, create a new project named PostgreSQLJDBC
and the main class named App
in the com.postgresqltutorial
package.

Second, add the PostgreSQL JDBC driver jar file to the project.
Third, you need to prepare the following:
- The address of the PostgreSQL database server e.g., localhost
- The database name e.g., dvdrental
- The username and password of the account that you will use to connect to the database.
For this information, you can construct the PostgreSQL JDBC connection string by using the following format:
jdbc:postgresql://<database_host>:<port>/<database_name>
Code language: SQL (Structured Query Language) (sql)
The <port> is optional.
In our example, the connection string is:
jdbc:postgresql://localhost/dvdrental
Code language: SQL (Structured Query Language) (sql)
To make it easier, we can define the attributes of the App class for storing connection string, user, and password:
private final String url = "jdbc:postgresql://localhost/dvdrental";
private final String user = "postgres";
private final String password = "<add your password>";
Code language: Java (java)
To establish a connection to the PostgreSQL database server, you call the getConnection
method of the DriverManager
class. This method returns a Connection
object.
The following connect()
method connects to the PostgreSQL database server and returns a Connection
object.
public Connection connect() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
System.out.println("Connected to the PostgreSQL server successfully.");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
Code language: Java (java)
The complete program for connecting to PostgreSQL database server is as follows:
package com.postgresqltutorial;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* @author postgresqltutorial.com
*/
public class App{
private final String url = "jdbc:postgresql://localhost/dvdrental";
private final String user = "postgres";
private final String password = "<add your password>";
/**
* Connect to the PostgreSQL database
*
* @return a Connection object
*/
public Connection connect() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
System.out.println("Connected to the PostgreSQL server successfully.");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
App app = new App();
app.connect();
}
}
Code language: Java (java)

So we can connect to the PostgreSQL database server successfully.
In this tutorial, we have shown you how to setup the Java development environment for working with the PostgreSQL databases.