How To Check Postgresql Version In Linux : PostgreSQL Server Version Check

Verifying your PostgreSQL version on a Linux server ensures you apply the correct configuration settings. If you are wondering how to check postgresql version in linux, you have come to the right place. This guide walks you through multiple methods, from simple commands to advanced queries, so you can quickly identify your PostgreSQL version.

Knowing your PostgreSQL version is essential for compatibility, security patches, and feature support. Whether you are a system administrator, developer, or database enthusiast, this article covers every practical approach. Let us jump straight into the methods.

How To Check Postgresql Version In Linux

This section provides the most direct ways to check your PostgreSQL version. You can use the command line, SQL queries, or system files. Each method is explained step by step.

Method 1: Using The Postgres Command

The fastest way is to run the postgres command with the --version flag. Open your terminal and type:

postgres --version

This outputs something like postgres (PostgreSQL) 14.5. If you get a “command not found” error, try the full path or use pg_config.

Alternatively, use postgres -V for a shorter version. Both commands work identically on most Linux distributions.

Method 2: Using Pg_config

The pg_config utility provides detailed build information. Run:

pg_config --version

This returns the version number like PostgreSQL 14.5. It is reliable even if the postgres binary is not in your PATH.

If pg_config is missing, install the postgresql-devel or libpq-dev package for your distribution.

Method 3: Using Psql Client

If you have access to the PostgreSQL server, connect with psql and run a version query. First, log in:

psql -U postgres

Then execute:

SELECT version();

This shows the full version string, including build info and platform. For just the version number, use:

SHOW server_version;

Both commands work from any client connection. They are ideal when you are already in the database environment.

Method 4: Checking The Initdb Command

The initdb command also reports its version. Run:

initdb --version

This is useful if you are setting up a new cluster and need to confirm the version before initialization.

Method 5: Inspecting The Data Directory

PostgreSQL stores version info in the PG_VERSION file inside the data directory. Typically located at /var/lib/postgresql/<version>/main or /var/lib/pgsql/data. View it with:

cat /var/lib/pgsql/data/PG_VERSION

This file contains only the major version number, like 14. It is a quick check if you know the data directory path.

Method 6: Using System Package Manager

Your Linux package manager can show installed PostgreSQL packages. For Debian/Ubuntu:

dpkg -l | grep postgresql

For Red Hat/CentOS/Fedora:

rpm -qa | grep postgresql

This lists all installed PostgreSQL components with their versions. It is helpful when you have multiple versions installed.

Method 7: Checking The Service Status

If PostgreSQL is running as a service, you can check its version via the service manager. For systemd:

systemctl status postgresql

The output often includes the version in the description line. For older SysV init, use:

service postgresql status

This method is indirect but works when you cannot run commands directly.

Common Issues And Troubleshooting

Sometimes the commands above fail. Here are solutions for typical problems.

Command Not Found Errors

If postgres --version returns “command not found”, PostgreSQL may not be installed or the binary is not in PATH. Check installation with:

which postgres

If nothing appears, install PostgreSQL using your package manager. For Ubuntu:

sudo apt install postgresql

For CentOS:

sudo yum install postgresql-server

After installation, retry the version command.

Multiple Versions Installed

You might have several PostgreSQL versions on one system. Use pg_lsclusters to list all clusters and their versions:

pg_lsclusters

This shows each cluster’s version, port, and status. It is invaluable for multi-version environments.

Permission Denied Errors

Some commands require root or postgres user privileges. Use sudo or switch to the postgres user:

sudo -u postgres psql -c "SELECT version();"

This avoids permission issues when accessing the data directory or running psql.

Why Version Matters For Configuration

Different PostgreSQL versions have different default settings, features, and deprecations. For example, max_connections defaults changed in version 14. Configuration files like postgresql.conf may have version-specific parameters.

Knowing your version helps you apply the correct tuning guides, avoid deprecated syntax, and plan upgrades. It also ensures compatibility with applications and extensions.

Using Version Information For Upgrades

When upgrading PostgreSQL, you must know the current version. Major upgrades (e.g., 13 to 14) require pg_upgrade or dump/restore. Minor upgrades (e.g., 14.4 to 14.5) are usually simpler.

Check the version before and after upgrade to confirm success. Use the commands above to verify.

Automating Version Checks In Scripts

You can incorporate version checking into bash scripts. For example:

#!/bin/bash
VERSION=$(psql -U postgres -t -c "SHOW server_version;" | xargs)
echo "PostgreSQL version is $VERSION"

This extracts the version without extra formatting. Use it for monitoring or deployment scripts.

Version Check For Remote Servers

If PostgreSQL runs on a remote Linux server, connect via SSH first:

ssh user@remote-server "postgres --version"

Or use psql remotely:

psql -h remote-server -U postgres -c "SELECT version();"

Ensure the remote server allows connections and you have credentials.

Checking Version Without Root Access

If you lack root privileges, you can still check the version using psql if it is installed. Run:

psql --version

This shows the client version. For server version, connect to a database you have access to:

psql -d yourdb -c "SHOW server_version;"

This works as long as you have database login rights.

Understanding Version Numbering

PostgreSQL uses a three-part versioning scheme: major.minor.patch (e.g., 14.5.2). The major version (14) indicates significant changes. Minor versions (5) include bug fixes. Patches (2) are rare and usually for security.

Some commands show only the major version, while SELECT version() gives the full string. Know which one you need for your task.

Using Docker Containers

If PostgreSQL runs in a Docker container, check the version inside the container:

docker exec -it container_name postgres --version

Or check the image tag: docker inspect container_name | grep "Image". This reveals the base image version.

Version Check For Source Compilations

If you compiled PostgreSQL from source, the version is embedded in the binary. Use postgres --version as usual. The PG_VERSION file in the data directory also works.

Common Mistakes To Avoid

  • Confusing client version with server version. psql --version shows client, not server.
  • Using pg_config from a different installation if multiple versions exist.
  • Forgetting to specify the correct port when connecting to a non-default instance.
  • Assuming the version in the package name matches the installed version (e.g., postgresql-14 package may be 14.5).

Frequently Asked Questions

How can I check the PostgreSQL version without logging into the database?

Use postgres --version or pg_config --version from the command line. No database connection needed.

What is the difference between SELECT version() and SHOW server_version?

SELECT version() returns a full string with build details. SHOW server_version returns only the version number like 14.5.

Why does postgres --version show a different version than psql --version?

postgres is the server binary, while psql is the client. They can be from different installations if multiple versions are present.

Can I check the PostgreSQL version from a remote client?

Yes, connect via psql -h remote_host -U user -c "SELECT version();" or use SSH to run postgres --version on the remote server.

How do I find the version if PostgreSQL is not in PATH?

Locate the binary with find / -name "postgres" -type f 2>/dev/null, then run it with --version. Alternatively, check the PG_VERSION file in the data directory.

Summary Of Commands

Here is a quick reference table for all methods:

  • postgres --version – Direct server version
  • pg_config --version – Build configuration version
  • psql -U postgres -c "SELECT version();" – Full version from database
  • cat /var/lib/pgsql/data/PG_VERSION – Major version from data directory
  • dpkg -l | grep postgresql – Package manager version (Debian)
  • rpm -qa | grep postgresql – Package manager version (Red Hat)
  • pg_lsclusters – List all clusters with versions

Each method has its use case. Choose the one that fits your access level and environment.

Now you know multiple ways to check your PostgreSQL version on Linux. Use this knowledge to maintain compatability, plan upgrades, and apply correct settings. If you encounter any issues, refer back to the troubleshooting section.

Remember to verify the version regularly, especially after updates or migrations. It is a small step that prevents big problems down the line.

We hope this guide answered your question about how to check postgresql version in linux. If you have further questions, leave a comment below.