Open In App

How to Check Your PostgreSQL Version

Last Updated : 27 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Knowing the specific version of PostgreSQL is vital for maintaining compatibility and utilizing new features. In this article, we will explain several methods to check our PostgreSQL version using the command line, the SQL shell, and the psql client. Also, it addresses common errors such as the "Command 'postgres' not found".

Checking PostgreSQL Version from the Command Line

When working with Postgres, the exact version we are using is essential for compatibility, performance tuning and taking advantage of the latest features and security improvements.

Checking the PostgreSQL version from the command line is one of the quickest methods available. Let's go over two commands that can help us determine the server and client versions.

1. Using the PSQL Command (Client Version)

The PSQL command is the PostgreSQL interactive terminal that allows us to communicate with the database. The --version option displays the installed version of the psql client.

To check the version of the PostgreSQL client installed on your system, use the following command

psql --version

Output:

Using-the-psql-Command
Using the psql Command

Explanation: This output indicates that the psql client version installed on my machine is 16.2, meaning i am using the client that interacts with a PostgreSQL 16.x server.

2. Using the postgres Command (Server Version)

The Postgres -V command directly checks the version of the PostgreSQL server. It’s useful for confirming the server version in case you are maintaining or configuring multiple versions of PostgreSQL.

To check the PostgreSQL server version, use the postgres command

postgres -V

Output:

Using-the-postgres-Command
Using the postgres Command


Explanation:

The output shows that my PostgreSQL server version is 16.2. It's important to note that the client and server versions should match or compatible to avoid communication errors.

Solve "Command 'postgres' not found" When Using postgres -V

Sometimes, when we try to run Postgres -V, we might encounter the "Command 'postgres' not found" error. This issue occurs either because PostgreSQL is not installed correctly or because the PATH environment variable is not configured. Here's how we can fix this:

Step 1. Verify Installation: Run the following command to verify if PostgreSQL is installed on your system.

This command lists the installed packages and should return a result if PostgreSQL is installed. If nothing is returned, we need to install PostgreSQL.

sudo apt list --installed | grep postgresql

Step 2. Add PostgreSQL to PATH: If PostgreSQL is installed but not found, add the PostgreSQL binary folder to your PATH:

export PATH=$PATH:/usr/lib/postgresql/14/bin

Step 3. Reinstall PostgreSQL: If the issue persists, reinstall PostgreSQL using the following command

sudo apt install postgresql

These steps will help resolve the error and ensure that the Postgres -V command works properly to check your server version.

Check Postgres Version from SQL Shell

The SQL Shell (PSQL) is another way to determine the PostgreSQL version. This method provides detailed information, including platform and compiler details. This query returns detailed information about the PostgreSQL server, including the version number, platform, and compiler used to build PostgreSQL.

Step 1: Log in to the PostgreSQL shell

Step 2: Run the following SQL command, This query returns the PostgreSQL version along with additional details like the OS platform and compiler version used to build the database.

SELECT version();

Output:

Check-Postgres-Version-from-SQL-Shell
Check Postgres Version from SQL Shell

Explanation:

The output shows that PostgreSQL version 16.2 is installed on a 64-bit system. Additionally, it provides details about the C++ compiler version used to build the database.

How to Check psql Client Version?

The psql client is a command-line interface that connects us to the PostgreSQL server. Checking the client version helps ensure that it's compatible with the server.

This command is similar to the one mentioned earlier and returns the version of the PostgreSQL psql client installed on your system. Run the following command to check the PSQLclient version,

psql --version

Solve "Command 'postgres' not found" when Checking psql Client Version

If we are checking the psql client version and encounter the "Command 'postgres' not found" error, the solution is similar to what we discussed earlier. Following these steps will help us to resolve the "command not found" issue when checking the PSQL client version.

Step 1: Check if psql is Installed: Run the following command to verify if the PSQL client is installed

sudo apt list --installed | grep psql

Step 2: If it's not listed: you can install it using

sudo apt install postgresql-client

Step 3: Add psql to PATH: If the client is installed but not accessible, add it to your system PATH using the following command

export PATH=$PATH:/usr/lib/postgresql/14/bin

Conclusion

In summary, quickly checking PostgreSQL versions via command line commands like psql --version and postgres -V is essential for effective database management. These methods enable users to confirm compatibility between client and server versions.


Next Article
Article Tags :

Similar Reads