How to get Postgresql version

In this article, we will how to find out the PostgreSQL version. It is simple process but with different methods. We can get the Server and client versions from Linux shell or using PostgreSQL commands.

PostgreSQL Server version from Linux shell:

locate bin/postgres | xargs -i xargs -t '{}' -V

Output:

postgres (PostgreSQL) 12.0 (Ubuntu 12.0-2.pgdg19.04+1)

psql --version 2>&1 | tail -1 | awk '{print $3}' | sed 's/\./ /g' | awk '{print $1 "." $2}'

Output:
12.0

psql -c "SELECT version();"

Output:

version --------------------------------------------------------------------------------------------------------------------------- PostgreSQL 12.0 (Ubuntu 12.0-2.pgdg19.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 8.3.0-6ubuntu1) 8.3.0, 64-bit To get PostgreSQL client version: To view the client version, again simply pass the -V flag to the psql client utility command:

psql -V

Output:

psql (PostgreSQL) 12.0 (Ubuntu 12.0-2.pgdg19.04+1)

PostgreSQL Server version using SQL statement:

SELECT version();

Output:

version --------------------------------------------------------------------------------------------------------------------------- PostgreSQL 12.0 (Ubuntu 12.0-2.pgdg19.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 8.3.0-6ubuntu1) 8.3.0, 64-bit

select version();

version --------------------------------------------------------------------------------------------------------------------------- PostgreSQL 12.0 (Ubuntu 12.0-2.pgdg19.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 8.3.0-6ubuntu1) 8.3.0, 64-bit

postgres=# show server_version;
server_version
----------------------------------
12.0 (Ubuntu 12.0-2.pgdg19.04+1)
(1 row)