How to Check Postgres Version

Postgres releases a new major version about once a year. For psql, it works best with servers of the same or an older major version.

There are multiple methods to check your PostgreSQL version, depending on your environment and access level. Here are the most common approaches:

1. Using psql Command-Line Interface

SELECT version();

This returns the full version string with build information.

SHOW server_version;

This returns just the version number (e.g., "15.4").

2. From the psql Shell Before Connecting

psql --version

or

psql -V

3. Through the PostgreSQL Server Binary

postgres --version

or

postgres -V

4. Using the pg_config Utility

pg_config --version

5. From the System Process

ps aux | grep postgres

The process list often shows the version in the binary path.

6. From PG_SETTINGS Table

SELECT setting FROM pg_settings WHERE name = 'server_version';

7. Checking Version in Application Code

Python (using psycopg2)

import psycopg2

conn = psycopg2.connect("dbname=postgres user=postgres")
cur = conn.cursor()
cur.execute("SELECT version();")
version = cur.fetchone()[0]
print(version)
cur.close()
conn.close()

Node.js (using node-postgres)

const { Client } = require('pg');
const client = new Client();
await client.connect();
const res = await client.query('SELECT version()');
console.log(res.rows[0].version);
await client.end();

Go (using lib/pq)

package main

import (
    "database/sql"
    "fmt"
    "log"

    _ "github.com/lib/pq"
)

func main() {
    connStr := "user=postgres dbname=postgres sslmode=disable"
    db, err := sql.Open("postgres", connStr)
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    var version string
    err = db.QueryRow("SELECT version()").Scan(&version)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(version)
}

Java (using JDBC)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class PostgresVersionCheck {
    public static void main(String[] args) {
        String url = "jdbc:postgresql://localhost:5432/postgres";
        String user = "postgres";
        String password = "yourpassword";

        try (Connection conn = DriverManager.getConnection(url, user, password);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT version()")) {

            if (rs.next()) {
                System.out.println(rs.getString(1));
            }
        } catch (SQLException e) {
            System.out.println("Error checking PostgreSQL version: " + e.getMessage());
        }
    }
}

8. From the Data Directory

Look for the PG_VERSION file in your data directory (path may vary depending on installation):

cat /var/lib/postgresql/data/PG_VERSION

References

Edit this page on GitHub