我在一个企业环境中(运行Debian Linux),没有自己安装它。我使用Navicat或phpPgAdmin访问数据库(如果有帮助的话)。我也没有运行数据库的服务器的shell访问权。
当前回答
公认的答案是很好的,但如果你需要与PostgreSQL版本进行编程交互,可能更好的做法是:
SELECT current_setting('server_version_num'); -- Returns 90603 (9.6.3)
-- Or using SHOW command:
SHOW server_version_num; -- Returns 90603 too
它将以整数形式返回服务器版本。这是如何在PostgreSQL源代码中测试服务器版本的,例如:
/*
* This is a C code from pg_dump source.
* It will do something if PostgreSQL remote version (server) is lower than 9.1.0
*/
if (fout->remoteVersion < 90100)
/*
* Do something...
*/
更多信息在这里和这里。
其他回答
不知道这有多可靠,但你可以自动获得两个版本令牌:
psql --version 2>&1 | tail -1 | awk '{print $3}' | sed 's/\./ /g' | awk '{print $1 "." $2}'
所以你可以构建到二进制文件的路径:
/usr/lib/postgresql/9.2/bin/postgres
只需将9.2替换为此命令。
使用CLI:
服务器版本:
$ postgres -V # Or --version. Use "locate bin/postgres" if not found.
postgres (PostgreSQL) 9.6.1
$ postgres -V | awk '{print $NF}' # Last column is version.
9.6.1
$ postgres -V | egrep -o '[0-9]{1,}\.[0-9]{1,}' # Major.Minor version
9.6
如果安装了多个PostgreSQL,或者得到"postgres: command not found"错误:
$ locate bin/postgres | xargs -i xargs -t '{}' -V # xargs is intentionally twice.
/usr/pgsql-9.3/bin/postgres -V
postgres (PostgreSQL) 9.3.5
/usr/pgsql-9.6/bin/postgres -V
postgres (PostgreSQL) 9.6.1
如果locate不起作用,试试find:
$ sudo find / -wholename '*/bin/postgres' 2>&- | xargs -i xargs -t '{}' -V # xargs is intentionally twice.
/usr/pgsql-9.6/bin/postgres -V
postgres (PostgreSQL) 9.6.1
虽然postmaster也可以代替postgres,但使用postgres更可取,因为postmaster是postgres的一个不推荐使用的别名。
客户端版本:
以postgres的身份登录。
$ psql -V # Or --version
psql (PostgreSQL) 9.6.1
如果安装了多个PostgreSQL:
$ locate bin/psql | xargs -i xargs -t '{}' -V # xargs is intentionally twice.
/usr/bin/psql -V
psql (PostgreSQL) 9.3.5
/usr/pgsql-9.2/bin/psql -V
psql (PostgreSQL) 9.2.9
/usr/pgsql-9.3/bin/psql -V
psql (PostgreSQL) 9.3.5
使用SQL:
服务器版本:
=> SELECT version();
version
--------------------------------------------------------------------------------------------------------------
PostgreSQL 9.2.9 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4), 64-bit
=> SHOW server_version;
server_version
----------------
9.2.9
=> SHOW server_version_num;
server_version_num
--------------------
90209
如果更好奇,try => SHOW all;。
客户端版本:
值得注意的是,可以在psql中执行shell命令,以显示路径中psql可执行文件的客户端版本。注意,正在运行的psql可能与路径中的psql不同。
=> \! psql -V
psql (PostgreSQL) 9.2.9
使用pgadmin4,双击Servers > server_name_here > Properties选项卡> Version:
3.5版本:
4.1、4.5版本:
在shell psql.exe中执行
\! psql -V
如果你在debian/ubuntu系统上有对服务器的shell访问权限(问题中提到op没有,但如果你有的话)
sudo apt-cache policy postgresql
它将输出已安装的版本,
postgresql:
Installed: 9.6+184ubuntu1.1
Candidate: 9.6+184ubuntu1.1
Version table:
*** 9.6+184ubuntu1.1 500
500 http://in.archive.ubuntu.com/ubuntu artful-updates/main amd64 Packages
500 http://in.archive.ubuntu.com/ubuntu artful-updates/main i386 Packages
500 http://security.ubuntu.com/ubuntu artful-security/main amd64 Packages
500 http://security.ubuntu.com/ubuntu artful-security/main i386 Packages
100 /var/lib/dpkg/status
9.6+184ubuntu1 500
500 http://in.archive.ubuntu.com/ubuntu artful/main amd64 Packages
500 http://in.archive.ubuntu.com/ubuntu artful/main i386 Packages
其中Installed: <version>是已安装的postgres包版本。