Installing PostgreSQL and setting up Hibernate

Installing PostgreSQL on Windows

Download PostgreSQL (I suggest the latest version).

Simply follow the installer which is pretty straightforward.

To check whether PostgreSQL is running:

Start Menu -> open Run -> type services.msc

Search for postgresql-x64(86)-version under Status should be Running.

Installing PostgreSQL on Ubuntu/Debian

I will be using Debian 8.7.0. You need to run the following commands (under sudo).

apt-get update
apt-get install postgresql postgresql-contrib

And you are done.

To check whether PostgreSQL is running:

service postgresql status

There should be a field called Active:  activein case no such field exists or is inactive there is problem with PostgreSQL

Adding Hibernate to your Maven project

Open pom.xml and under the <dependencies> tag add the following:


The first dependency adds Hibernate and the second adds the PostgreSQL driver

Setting up Hibernate

Create a class called HibernateUtility

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtility {

 private HibernateUtility(){
 // the class cannot be instantiated from outside

 // On initialization the Hibernate's SessionFactory will be build, I have ran into cases when this needs to be done manually
 private static final SessionFactory sessionFactory = buildSessionFactory();

 private static SessionFactory buildSessionFactory() {
 try {
 // Creating Hibernate's configuration
 Configuration configuration = new Configuration();
 // Setting up which database driver we want to use
 configuration.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver");
 // The url for connecting to the database
 configuration.setProperty("hibernate.connection.url", "jdbc:postgresql://HOST:IP/DATABASE");
 // Database username
 configuration.setProperty("hibernate.connection.username", "USERNAME");
 // Database password
 configuration.setProperty("hibernate.connection.password", "PASSWORD");
 // Which database dialect should be used for the queries, usually it is done automatically
 configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");

 // shows the SQL Query, useful for debugging, sometimes because of 3rd party libraries you may have to set manually Logger
 configuration.setProperty("hibernate.show_sql", "true");

 // adds an Entity class which uses @Annotations

// builds the factory and returns it
 StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
 return configuration.buildSessionFactory(;
 } catch (Throwable ex) {
// throws an exception when there is problem(cannot connection to the database, driver is missing and etc)
 throw new ExceptionInInitializerError(ex);

// returns the Session Factory and ready to be used!
 public static SessionFactory getSessionFactory() {
 return sessionFactory;


If you have any questions or suggestions, feel free to leave a comment.
Become a Patron!