How To Check Postgres Version In Linux – PostgreSQL Version Query Command

Knowing your PostgreSQL version in Linux is essential for compatibility checks during database upgrades. If you are wondering how to check postgres version in linux, you have come to the right place. This guide will show you multiple quick and reliable methods to find your PostgreSQL version using the command line, regardless of your Linux distribution.

PostgreSQL, often called Postgres, is a powerful open-source relational database system. Different versions come with different features, security patches, and performance improvements. Checking your version helps you plan upgrades, debug issues, and ensure your applications run smoothly.

We will cover several ways to check the version, from simple commands to querying the database itself. Each method is tested on Ubuntu, Debian, CentOS, Fedora, and other common Linux systems. Let’s get started.

How To Check Postgres Version In Linux

This section provides the most straightforward and commonly used methods. You do not need to be a database administrator to use these commands. Most of them work directly from your terminal.

Method 1: Using The Postgres Command With The –Version Flag

The simplest way is to use the postgres binary itself. This command works if PostgreSQL is installed and the binary is in your system’s PATH.

  1. Open your terminal.
  2. Type the following command and press Enter:
postgres --version

You will see output similar to this:

postgres (PostgreSQL) 14.5

If you get a “command not found” error, the binary might not be in your PATH. Try the full path instead, which is often /usr/lib/postgresql/14/bin/postgres or /usr/pgsql-14/bin/postgres. Replace “14” with your installed version number if you know it.

Method 2: Using The Psql Command

psql is the interactive terminal for PostgreSQL. You can check the version without connecting to a database.

  1. Open your terminal.
  2. Run this command:
psql --version

The output will look like:

psql (PostgreSQL) 14.5

This shows the version of the client tools. It usually matches the server version, but not always. If you have multiple versions installed, this shows the one that runs first in your PATH.

Method 3: Using The Initdb Command

initdb is used to create a new database cluster. It also shows its version when called with the --version flag.

initdb --version

Output example:

initdb (PostgreSQL) 14.5

This method is less common but works well if you have the server development packages installed.

Method 4: Checking The Installed Package Version

Your Linux package manager can tell you exactly which PostgreSQL package is installed. This is helpful if you want to see the exact package version, including patch levels.

On Debian Or Ubuntu (APT)

dpkg -l | grep postgresql

Or use apt:

apt list --installed | grep postgresql

You will see lines like:

ii  postgresql-14  14.5-0ubuntu0.22.04.1  amd64

On Red Hat, CentOS, Or Fedora (YUM Or DNF)

rpm -qa | grep postgres

Or with dnf:

dnf list installed | grep postgres

Output example:

postgresql14-server-14.5-1PGDG.rhel8.x86_64

On SUSE (Zypper)

zypper se --installed-only postgresql

Method 5: Querying The Database Server Directly

If PostgreSQL is running, you can connect to it and ask for the version. This method gives you the exact server version, which is the most reliable.

  1. Connect to the database using psql. You may need to specify a user and database:
sudo -u postgres psql

Or if you have a regular user with access:

psql -U your_username -d your_database
  1. Once inside the psql prompt, run this SQL command:
SELECT version();

You will see a detailed string:

PostgreSQL 14.5 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, 64-bit

This includes the operating system, compiler, and architecture information.

Alternatively, you can use a shorter query:

SHOW server_version;

This returns just the version number, like 14.5.

Method 6: Checking The PostgreSQL Service Status

Sometimes you want to know which version of the server is actually running. You can check the service status and see the version in the process list.

systemctl status postgresql

Or for a specific version:

systemctl status postgresql@14-main

Look for the line that shows the executable path. It often includes the version number, like /usr/lib/postgresql/14/bin/postgres.

You can also use ps to see running processes:

ps aux | grep postgres

The first line usually shows the postmaster process with the version in the path.

Method 7: Checking The File System For Version Files

PostgreSQL stores its version number in a file called PG_VERSION inside the data directory. This is useful if you cannot start the server.

  1. Find your data directory. Common locations are /var/lib/postgresql/14/main or /var/lib/pgsql/14/data.
  2. Run this command:
cat /var/lib/postgresql/14/main/PG_VERSION

It will output just the version number, like 14.

Note: This file only shows the major version (e.g., 14), not the minor version (e.g., 14.5).

Understanding The Output

When you check the version, you might see different formats. Here is what they mean:

  • Major version: The first number, like 14. This indicates significant feature changes.
  • Minor version: The number after the dot, like 5. This includes bug fixes and security patches.
  • Patch level: Sometimes a third number, like 14.5.1, indicates a hotfix release.

For example, version 14.5 means major version 14, minor version 5. Always use the latest minor version for your major version to stay secure.

Common Issues And Troubleshooting

Sometimes you may run into problems when trying to check the version. Here are some frequent issues and how to fix them.

Command Not Found Error

If you see command not found: postgres, the PostgreSQL binaries are not in your PATH. This often happens when you installed PostgreSQL from source or used a non-standard location.

Solutions:

  • Use the full path, like /usr/pgsql-14/bin/postgres --version.
  • Add the bin directory to your PATH temporarily: export PATH=/usr/pgsql-14/bin:$PATH.
  • Check if PostgreSQL is installed at all using your package manager.

Multiple Versions Installed

It is possible to have multiple PostgreSQL versions on the same system. The postgres --version command shows the one that appears first in your PATH. To check a specific version, use its full path.

For example, if you have version 13 and 14 installed:

/usr/lib/postgresql/13/bin/postgres --version
/usr/lib/postgresql/14/bin/postgres --version

Permission Denied When Using Psql

If you get a permission error when trying to connect with psql, you likely need to use the postgres system user.

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

This runs the command as the postgres user and exits immediately.

PostgreSQL Not Running

If the server is not running, you cannot query it directly. Use the command-line methods or check the PG_VERSION file instead.

Why Checking The Version Matters

Knowing your PostgreSQL version is not just a technical curiosity. It has practical implications for your work.

  • Compatibility: Applications and libraries often require a specific PostgreSQL version. Running an unsupported version can cause crashes or data corruption.
  • Security: Older versions may have known vulnerabilities. The PostgreSQL Global Development Group releases security patches regularly. You should always run the latest minor version.
  • Feature availability: New features like native partitioning, parallel query execution, or improved indexing are only available in certain versions. If you need a feature, you must upgrade.
  • Support lifecycle: Each major version is supported for five years. After that, you must upgrade to continue receiving security updates.

Automating Version Checks In Scripts

If you manage multiple servers, you might want to check the PostgreSQL version automatically. Here are some one-liners for scripts.

To get just the version number from the command line:

postgres --version | awk '{print $3}'

This extracts the third field, which is the version number like 14.5.

To check from a remote server using SSH:

ssh user@server "postgres --version"

To query the server version without interactive login:

psql -h localhost -U postgres -c "SELECT version();" -t -A

The -t flag removes headers, and -A removes alignment.

Checking Version In Docker Containers

If you run PostgreSQL in a Docker container, you can check the version from inside the container.

docker exec -it your_container_name postgres --version

Or connect to the container and run commands:

docker exec -it your_container_name bash
postgres --version

You can also check the image tag to know the version. For example, postgres:14 means version 14.x.

Checking Version On Different Linux Distributions

While the commands are mostly the same, the package names and default paths vary slightly.

Ubuntu And Debian

Default install location: /usr/lib/postgresql/14/bin/. Packages are named postgresql-14, postgresql-client-14, etc.

CentOS And RHEL

Default install location: /usr/pgsql-14/bin/. Packages from the PostgreSQL YUM repository are named postgresql14-server, postgresql14-contrib, etc.

Fedora

Similar to CentOS but uses DNF. The default module stream may provide version 13 or 14.

Arch Linux

Default install location: /usr/bin/. The package is simply called postgresql.

OpenSUSE

Default install location: /usr/lib/postgresql14/bin/. Packages are postgresql14, postgresql14-server, etc.

Frequently Asked Questions

1. How Can I Check The PostgreSQL Version Without Logging Into The Database?

You can use the postgres --version or psql --version command directly from the terminal. These do not require a database connection.

2. What Is The Difference Between SELECT Version() And SHOW Server_version?

SELECT version() returns a detailed string with the version, operating system, compiler, and architecture. SHOW server_version returns just the version number, like 14.5.

3. Why Does psql –Version Show A Different Version Than The Server?

psql --version shows the version of the client tools. If you have multiple PostgreSQL versions installed, the client may be from a different version than the running server. Always check the server version directly with SELECT version() for accuracy.

4. Can I Check The PostgreSQL Version From A Remote Machine?

Yes, you can use psql -h remote_host -U username -c "SELECT version();" if the remote server allows connections. Alternatively, use SSH to run commands on the remote server.

5. How Do I Find The PostgreSQL Version If The Server Is Not Running?

Check the PG_VERSION file in the data directory, or use the package manager to see which version is installed. For example, dpkg -l | grep postgresql on Debian or rpm -qa | grep postgres on Red Hat.

Best Practices For Version Management

Keeping track of your PostgreSQL version is part of good database administration. Here are some tips.

  • Document your version: Record the version number and install date in your system documentation.
  • Monitor for updates: Subscribe to the PostgreSQL announce mailing list or use your package manager’s update notifications.
  • Test upgrades: Before upgrading to a new major version, test your applications in a staging environment.
  • Use version control: If you use configuration management tools like Ansible or Puppet, include the PostgreSQL version in your inventory.
  • Backup before changes: Always take a full backup before upgrading PostgreSQL, especially across major versions.

Conclusion

Now you know multiple ways to check your PostgreSQL version in Linux. Whether you prefer a quick command-line check or a detailed database query, you have the tools to get the information you need. Remember to keep your PostgreSQL installation up to date for security and performance. If you ever need to troubleshoot or plan an upgrade, checking the version is your first step. Practice these commands today so they become second nature when you need them.