1

Is it possible to find version of PostgreSQL using Java and also to start the PostgreSQL server using Java code?

To find version we have PG_VERSION file which has version under PostgreSQL data directory but we cannot rely on it since this file can be edited.

5 Answers 5

3

You can also find the version of the server using DatabaseMetaData. It has helper methods for getting information about the server. getDatabaseProductVersion(), getDatabaseMajorVersion(), getDatabaseMinorVersion() will give you the database server version.

Sign up to request clarification or add additional context in comments.

Comments

2

Quick googling reveals the query:

select version();

Run it through JDBC, you have java anyway. As for running the the postgres server itself, I think the old way Runtime.exec/ProcessBuilder will do the job. You may want to create some kind of script above it but that the way it should work, I guess, there is no other way.

If you're running on linux machine, maybe you should install the server as a service and run it respectively, but from the point view of Java (if you really want to run it from java) its still an invocation of external process.

Hope this helps

Comments

0

If PG_VERSION is edited the server won't start. Don't do that. In general, the PostgreSQL data-dir is off limits for direct editing.

At run-time you can use SELECT version() to get the version of the running server.

The server can be started and stopped with pg_ctl. You can check the version of the server binaries whether or not the server is running postgres --version or pg_ctl --version.

The location of the binaries and datadir is something you can control in terms of how you install the server.

2 Comments

Both the commands postgres --version and pg_ctl --version works only in Unix/Linux. Do we have any command to do the same in Windows or Mac ?
Er... those commands will work just fine on Mac OS X or Windows. Perhaps your confusion arises from the executables not being on the PATH? Either invoke them with a full path, like C:\Program Files\PostgreSQL\9.2\bin\postgres.exe -version, or add the installation directory to the PATH so you can invoke them without specifying a path on the command line. That's not PostgreSQL-specific, that's just very basic computers/operating systems stuff.
0

Running the query : select version()
would give you the exact version of the posgresql

Comments

0

You can also use DatabaseMetaData interface. It's much more general, not only for postgreSQL and gives you much more than just the database version. Here's an example on how to use it:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

/**
 * @author Binyamin Regev on on 30/11/2016.
 * @version 1.0
 * @since 1.8
 */

public class DatabaseMetaDataHelper {

    static Connection connection = null;
    static DatabaseMetaData metadata = null;


    public DatabaseMetaDataHelper() throws SQLException{

        boolean success = initJDBCDriver();
        if (!success) { throw new SQLException("INIT JDBC DRIVER FAILED!"); }

        success = connectToDatabase();
        if (!success) { throw new SQLException("Connect to database FAILED"); }

        connection = databaseConnection.getConnection();

        try {
            metadata = databaseConnection.getConnection().getMetaData();
        } catch (SQLException e) {
            System.err.println("There was an error getting the metadata: " + e.getMessage());
            e.printStackTrace();
        }
    }

    public boolean initJDBCDriver() {
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("Where is your PostgreSQL JDBC Driver? Include in your library path!");
            e.printStackTrace();
            return false;
        }

        System.out.println("PostgreSQL JDBC Driver Registered!");
        return true;
    }

    public boolean connectToDatabase() {
        try {
            connection = DriverManager.getConnection(
                    "jdbc:postgresql://127.0.0.1:5432/postgres",
                    "postgres",
                    "admin");

        } catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return false;
        }

        if (connection == null) {
            System.out.println("Failed to make connection!");
            return false;
        }

        System.out.println("You made it, take control your database now!");
        return true;
    }

    public static Connection getConnection() {
        return connection;
    }

    public static String getDatabaseProductName() {
        try {
            return metadata.getDatabaseProductName();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getDriverName() {
        try {
            return metadata.getDriverName();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getUserName() {
        try {
            return metadata.getUserName();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getDatabaseProductVersion() {
        try {
            return metadata.getUserName();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getDriverVersion() {
        try {
            return metadata.getDriverVersion();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getURL() {
        try {
            return metadata.getURL();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int getDriverMinorVersion() {
        return metadata.getDriverMinorVersion();
    }

    public static int getDriverMajorVersion() {
        return metadata.getDriverMajorVersion();
    }

    public static int getDatabaseMinorVersion() {
        try {
            return metadata.getDatabaseMinorVersion();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public static int getDatabaseMajorVersion() {
        try {
            return metadata.getDatabaseMajorVersion();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public static int getJDBCMajorVersion() {
        try {
            return metadata.getJDBCMajorVersion();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public static int getJDBCMinorVersion() {
        try {
            return metadata.getJDBCMinorVersion();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public static void main(String[] args) {
        try {
            DatabaseMetaDataHelper helper = new DatabaseMetaDataHelper();
            int result = helper.connectToDatabase();

            System.out.println("Database product name = " + getDatabaseProductName());
            System.out.println("Database product version = " + getDatabaseProductVersion());
            System.out.println("Database User name = " + getUserName());
            System.out.println("Driver name = " + getDriverName());
            System.out.println("Driver version = " + getDriverVersion());
            System.out.println("URL = " + getURL());
            System.out.println("Driver major version = " + getDriverMajorVersion());
            System.out.println("Driver minor version = " + getDriverMinorVersion());
            System.out.println("Databse major version = " + getDatabaseMajorVersion());
            System.out.println("Databse minor version = " + getDatabaseMinorVersion());
            System.out.println("JDBC major version = " + getJDBCMajorVersion());
            System.out.println("JDBC minor version = " + getJDBCMinorVersion());

            /*
             * Print all the tables of the database scheme,
             * with their names and structure
             */
            getColumnsMetadata(getTablesMetadata());
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            System.err.println("There was an error retrieving the metadata properties: "+ e.getMessage());
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.