PostgreSQL - Installing JDBC driver
Asked Answered
S

5

11

I'm having a hard time working out how I should be installing the JDBC driver for PostgreSQL on my debian 6.0 server. I have moved the driver .jar into the following directory:

/usr/local/pgsql/share/java/postgresql.jar. 

Then the tutorials talk about using this code:

Class.forName("org.postgresql.Driver");

However, since I am new to postgreSQL I have no idea where I should be putting this line, or if this is even correct.

My question is, short of moving the jar file to this location, what do I actually need to do in order to install the JDBC driver on my postgreSQL installation?


EDIT: This is my setup:

Server 1: Tomcat + SOLR

Server 2: PostgreSQL with JDBC driver

SOLR on server 1 queries postgreSQL on server 2 via the JDBC driver

Stripteaser answered 13/12, 2012 at 12:36 Comment(3)
Drivers implementing the JDBC 4.0 specification no longer require the manual loading of the driver class (Class.forName(...)). So if this is your case, it should suffice with putting the driver in your class path as @Tomas suggested in his answer.Steepen
What happens if my appliocation, in this case, SOLR, is on a different server to postgreSQL? Should I be putting this class path on the solr server?Stripteaser
Or is my app instead Postgresql?Stripteaser
D
7

It is best to install your PostgreSQL driver into tomcat\lib folder. Just copy the driver jar to PATH_TO_TOMCAT\lib

It is not a good idea to add things to the system CLASSPATH because you can end in class loader hell. Here is an example of how you end up in jar / classpath hell.

  • Suppose the current app uses postgres 9.1 and you setup the driver on the system CLASSPATH
  • You decide to run another app on that box which talks to a newer version of postgres lets say version 9.2
  • Because you are using the system classpath app 2 will end up using the old driver because the SYSTEM classpath tends to take precedence over an applications classpath unless the app launcher script sets CLASSPATH="" to empty out the system classpath or uses a custom class loader that does not do parent-first class loading.

See http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html

Demosthenes answered 13/12, 2012 at 19:30 Comment(0)
A
3

That driver has to be on your classpath. You can use this command

java -cp /usr/local/pgsql/share/java/postgresql.jar my.app.MainClass

or you can copy the library into your project structure.

Then you can create connections as the tutorials say...

Allopathy answered 13/12, 2012 at 12:41 Comment(4)
Thank you for the reply. That is what I don't get though. My "app" is SOLR running on another server. What should I be using as my app in your example?Stripteaser
@JamesWillson: If SOLR is your "app" then you should edit your question and explain your setup. Running embedded inside another framework/container/server involves a completely different setup than running a small, standalone app.Edaedacious
Thank you both for the comments. I am really a bit out of my depth here so I'm sorry for not clarifying everything. I have added more information on my original post. Please could you see if this brings any clarity?Stripteaser
@JamesWillson As I see it, server 2 does not need the driver. The database machine is completely standalone and only provides an interface which you can connect on. So put the JDBC driver in the Tomcat's lib directory and you should be fine...Allopathy
E
2

Install all packages:

# apt-get install libpostgresql-jdbc-java libpostgresql-jdbc-java-doc

To set Java Environment for all users, add/edit /etc/environment:

JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"
CLASSPATH=".:/usr/share/java/mysql.jar:/usr/share/java/postgresql-jdbc4.jar"

Note: change /usr/lib/jvm/java-8-openjdk-amd64 with your JDK

Note: if you prefered to use postgresql-jdbc3, replace /usr/share/java/postgresql-jdbc4.jar with /usr/share/java/postgresql.jar

Test your connection using this code:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.Properties;

class TestDB {

    /*

      /usr/share/java

      http://dev.mysql.com/doc/connector-j/5.1/en/

      https://jdbc.postgresql.org/documentation/documentation.html

    */

   static Connection conn = null;   

   public static void main(String[] args) {
      // PostgreSQL

      try {

         System.out.println("Loading Class org.postgresql.Driver");

         Class.forName("org.postgresql.Driver");

         System.out.println("Loading org.postgresql.Driver Successful");

         String url = "jdbc:postgresql://localhost/database";

         Properties props = new Properties();

         props.setProperty("user","user");

         props.setProperty("password","password");

         props.setProperty("ssl","true");

         conn = DriverManager.getConnection(url, props); 

         // or

         url = "jdbc:postgresql://localhost/database?user=user&password=password&ssl=true";

         Connection conn = DriverManager.getConnection(url);

         // Do something with the Connection

         System.out.println("Test Connection Successful");

      } catch (SQLException ex) {

         // handle any errors

         System.out.println("SQLException: " + ex.getMessage());

         System.out.println("SQLState: " + ex.getSQLState());

         System.out.println("VendorError: " + ex.getErrorCode());

      } catch (ClassNotFoundException ex) {

         System.out.println("Class Not Found: " + ex.getMessage());

      }

   }

}

Note: change database, user and passwrod with your configuration

http://www.garasiku.web.id/web/joomla/index.php/java/112-debian-jessie-installing-openjdk-8-mysql-jdbc-and-postgresql-jdbc

Embassy answered 1/11, 2016 at 4:33 Comment(0)
C
1

In your IDE (Idea, Eclipse, etc) you need to add that path as a library.

Alternatively, you can compile and execute from the command-line, if you define a CLASSPATH variable which includes that.

export CLASSPATH=/usr/local/pgsql/share/java/postgresql.jar
javac -classpath $CLASSPATH MyDBApp.java
java -cp $CLASSPATH MyDBApp
Chancelor answered 13/12, 2012 at 13:18 Comment(0)
P
-1

For resolving this problem when Dbeaver connecting to Postgrsql Driver first 1.You need to download your .jar file (for example postgresql-42.7.2.jar) from https://jdbc.postgresql.org/download/ link

  1. Then you must go to 'Edit Driver' Postgresql

  2. Then click the Properties

  3. Click the Classpath

  4. you will see the + button(Add)

  5. And add your local downloaded path here to GlobalLibraries input, (For example C:\Users\Downloads\postgresql-42.7.2.jar)

7.At the end click Apply and Close button

Happy Coding)

Phlebitis answered 2/8 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.