Postgresql server connection works with pgAdmin but doesn’t work with myBatis : Part I

Today I was trying to debug a failure in connecting to a postgresql server. As the title says, it works when attempting to connect with pgAdmin, but fails with mybatis. Before we go in detail, here is a short summary for the busy reader.

If your database connection works with pgAdmin it has nothing to assert that it will work with your MyBatis setup. Because pgAdmin is a python client for the postgresql that uses a different mechanism to connect to the server, whereas MyBatis uses the Postgresql JDBC connecter.

Now let's discuss this in detail and see if we can understand the real issue (in my case)

pgAdmin is a database client developed on top of python and it uses the psycopg2 python module to connect to the postgresql server, whereas mybatis relies on a postgresql driver for JDBC to connect. I wrote a small script in python and in java to test the connection (as shown below)


import psycopg2
    conn = psycopg2.connect("dbname='template1' user='dbuser' host='localhost' password='dbpass'")
    print "I am unable to connect to the database"

Note : To run the above you need to install the psycopg2 module (e.x : python – m pip install psycopg2)

Java: (

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class JDBCExample {
    public static void main(String[] argv) throws Exception {
    System.out.println("PostgreSQL JDBC Driver Registered!");
    Connection connection = null;
    connection = DriverManager.getConnection(
     "jdbc:postgresql://server:port/db", "username","password");
    if (connection != null) {
        System.out.println("You made it, take control your database now!");
    } else {
        System.out.println("Failed to make connection!");

Note : To run the above code you need to have the JDBC connector for Postgresql and invoke it from command line as follows :


java –cp <driver jar location>;<your classpath location> JDBCExample

and got this exception

-------- PostgreSQL JDBC Connection Testing ------------ PostgreSQL JDBC Driver Registered! Connection Failed! Check output console org.postgresql.util.PSQLException: The connection attempt failed. at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl( at org.postgresql.core.ConnectionFactory.openConnection( at org.postgresql.jdbc.PgConnection.<init>( at org.postgresql.Driver.makeConnection( at org.postgresql.Driver.connect( at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at JDBCExample.main( Caused by: Permission denied: connect at Method) at Source) at Source) at Source) at Source) at Source) at Source) at Source) at org.postgresql.core.PGStream.<init>( at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl( ... 7 more

On running the above two code snippets, it's clear that the python code succeeds and the java code fails with the exception (PSQLException). That for sure explained one thing, pgAdmin is not a likely candidate to help debug issues with myBatis. In next version of my post I will be blogging the reason behind the failure. Since this issue happens to me randomly am using the random instances to debug it, whilst it's a bit hard to isolate possible failing points. From top of my head one suspect would be related to network connections, because other than that my system is non user intervened w.r.t settings, in between these successful and failed connections.

Have a good time J