How to View Which Postgres Version is Running

Data Tutorial

Viewing which version (or versions) of PostgreSQL is active on your system is a reasonably simple process, but there are a number of methods that can be used. In this tutorial we’ll briefly explore four methods for finding your version of PostgreSQL, including both the client and the server versions.

Note: As per usual when working with PostgreSQL, be sure you are logged into your shell prompt from a non-root user with Postgres privileges for these commands to function properly.

Using the Shell Command Line

Both the server and client Postgres installations can be queried using their respective command line utilities.

Viewing the Server Version

To find the Postgres server version from the shell command line, simply issue a postgres command with the -V flag (for version):

$ postgres -V
postgres (PostgreSQL) 9.3.10

In the event that the postgres command is not found, you may need to locate the directory of the utility. This can be done by issuing the locate bin/postgres command:

$ locate bin/postgres
/usr/lib/postgresql/9.3/bin/postgres

Now with the direct path to the postgres utility, you can call it with the -V flag as illustrated above:

$ /usr/lib/postgresql/9.3/bin/postgres -V
postgres (PostgreSQL) 9.3.10

Viewing the Client Version

To view the client version, again simply pass the -V flag to the psql client utility command:

$ psql -V
psql (PostgreSQL) 9.3.10

Similar to the above, if you cannot find the utility – or have multiple installations of PostgreSQL on that machine – you can easily locate psql:

$ locate bin/psql
/usr/bin/psql
/usr/lib/postgresql/9.3/bin/psql

Then issue a direct call to the located psql utility for the version:

$ /usr/lib/postgresql/9.3/bin/psql -V
psql (PostgreSQL) 9.3.10

Using SQL

It is also possible to determine the Postgres version from within a Postgres SQL prompt via a simple SQL statement.

Viewing the Server Version

To determine the server version using an SQL statement, simply issue the SELECT version(); command:

=# SELECT version();
                                              version
------------------------------------------------------------------------------------------------------
PostgreSQL 9.3.10 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2, 64-bit
(1 row)

You’ll see the full version output information as seen in the example above.

Alternatively, you can query for the specific server version, in the standard major.minor.patch format, by using the SHOW command:

=# SHOW server_version;
 server_version
----------------
 9.3.10
 (1 row)

SHOW is used to display current run-time parameters, which are essentially just a table of name/setting pairs. By issuing the SHOW server_version; statement above, we’re asking Postgres to retrieve the current parameter value of server_version, which of course is the version of PostgreSQL currently running.

Viewing the Client Version

In a similar vein, we can also issue a short SQL statement from a PostgreSQL prompt to query the client version of psql. However, it’s worth noting that this is purely for convenience, as we’re effectively just asking Postgres to issue a command on the shell prompt, but from within the PostgreSQL prompt itself.

This is performed by using the \! flag while connected to the client, followed by the statement we wish to issue:

=# \! psql -V
psql (PostgreSQL) 9.3.10

Just as before when we were issuing this command directly from the shell prompt, psql -V may return your client version as above, or the path may not be found.

With that we’ve covered four simple yet effective ways to detect both the server and client versions of Postgres.