如何通过 Linux 脚本检查是否安装了 PostgreSQL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5803262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check if PostgreSQL is installed or not via Linux script?
提问by Nirmal- thInk beYond
I want to check in a script if PostgreSQL is installed or not on Linux and print the result. Any suggestions on how to do the check?
我想检查一个脚本是否在 Linux 上安装了 PostgreSQL 并打印结果。有关如何进行检查的任何建议?
采纳答案by campo
What about trying the which
command?
试试这个which
命令怎么样?
If you were to run which psql
and Postgres is not installed there appears to be no output. You just get the terminal prompt ready to accept another command:
如果您要运行which psql
并且未安装 Postgres,则似乎没有输出。您只需准备好终端提示即可接受另一个命令:
> which psql
>
But if Postgres is installed you'll get a response with the path to the location of the Postgres install:
但是如果安装了 Postgres,你会得到一个包含 Postgres 安装位置路径的响应:
> which psql
/opt/boxen/homebrew/bin/psql
Looking at man which
there also appears to be an option that could help you out:
查看man which
似乎还有一个选项可以帮助您:
-s No output, just return 0 if any of the executables are found, or
1 if none are found.
So it seems like as long as whatever scripting language you're using can can execute a terminal command you could send which -s psql
and use the return value to determine if Postgres is installed. From there you can print that result however you like.
因此,似乎只要您使用的任何脚本语言都可以执行您可以发送的终端命令which -s psql
并使用返回值来确定是否安装了 Postgres。从那里你可以打印你喜欢的结果。
I do have postgres installed on my machine so I run the following
我的机器上确实安装了 postgres,所以我运行以下命令
> which -s psql
> echo $?
0
which tells me that the command returned 0, indicating that the Postgres executable was found on my machine.
这告诉我该命令返回 0,表明在我的机器上找到了 Postgres 可执行文件。
回答by Draco Ater
If it is debian based.
如果是基于debian的。
aptitude show postgresql | grep State
But I guess you can just try to launch it with some flag like --version
, that simply prints some info and exits.
但是我想您可以尝试使用诸如 之类的标志来启动它--version
,它只是打印一些信息并退出。
Updatedusing "service postgres status". Try:
使用“服务 postgres 状态”更新。尝试:
service postgres status
if [ "$?" -gt "0" ]; then
echo "Not installed".
else
echo "Intalled"
fi
回答by Peter Eisentraut
There is no straightforward way to do this. All you can do is check with the package manager (rpm, dpkg) or probe some likely locations for the files you want. Or you could try to connect to a likely port (5432) and see if you get a PostgreSQL protocol response. But none of this is going to be very robust. You might want to review your requirements.
没有直接的方法可以做到这一点。您所能做的就是检查包管理器(rpm、dpkg)或探查您想要的文件的一些可能位置。或者您可以尝试连接到可能的端口 (5432) 并查看是否收到 PostgreSQL 协议响应。但这一切都不会非常稳健。您可能想要查看您的要求。
回答by Finn
Go to bin directory of postgres db such as /opt/postgresql/bin
& run below command :
转到 postgres db 的 bin 目录,例如/opt/postgresql/bin
& 运行以下命令:
[...bin]# ./psql --version
psql (PostgreSQL) 9.0.4
Here you go . .
干得好 。.
回答by Craig Ringer
There is no single simple way to do it, because PostgreSQL might be installed and set up in many different ways:
没有一种简单的方法可以做到这一点,因为 PostgreSQL 可能以多种不同的方式安装和设置:
- Installed from source in a user home directory
- Installed from source into
/opt
or/usr/local
, manually started or started by an init script - Installed from distributor
rpm
/deb
packages and started via init script - Installed from 3rd party
rpm
/deb
packages and started via init script - Installed from packages but notset to start
- Client installed, connecting to a server on a different computer
- Installed and running but not on the default
PATH
or default port
- 从用户主目录中的源安装
- 从源代码安装到
/opt
or/usr/local
,手动启动或由 init 脚本启动 - 从分销商
rpm
/deb
软件包安装并通过初始化脚本启动 - 从第 3 方
rpm
/deb
软件包安装并通过 init 脚本启动 - 从包安装但未设置为启动
- 客户端已安装,连接到另一台计算机上的服务器
- 已安装并运行,但不在默认
PATH
或默认端口上
You can't rely on psql
being on the PATH
. You can't rely on there being only one psql
on the system (multiple versions might be installed in different ways). You can't do it based on port, as there's no guarantee it's on port 5432, or that there aren't multiple versions.
你可以不依赖于psql
正对PATH
。您不能依赖系统上只有一个psql
(多个版本可能以不同的方式安装)。你不能根据端口来做,因为不能保证它在端口 5432 上,或者没有多个版本。
Prompt the user and ask them.
提示用户并询问他们。
回答by netfluence
And if everything else fails from these great choice of answers, you can always use "find" like this. Or you may need to use sudo
如果其他一切都因这些出色的答案选择而失败,您始终可以像这样使用“查找”。或者您可能需要使用 sudo
If you are root, just type $$> find / -name 'postgres'
如果你是 root,只需输入 $$> find / -name 'postgres'
If you are a user, you will need sudo priv's to run it through all the directories
如果您是用户,则需要 sudo priv 才能在所有目录中运行它
I run it this way, from the /
base to find the whole path that the element is found in. This will return any files or directories with the "postgres" in it.
我以这种方式运行它,从/
基础开始查找元素所在的整个路径。这将返回其中包含“postgres”的任何文件或目录。
You could do the same thing looking for the pg_hba.conf
or postgresql.conf
files also.
你也可以做同样的事情来寻找pg_hba.conf
orpostgresql.conf
文件。
回答by Roland Shield
If you are running Debian Linux (or derivative) and if you have a postive return with > which psql
, then simply type psql -V
(capital "V") and you will get a return like: psql (PostgreSQL) 9.4.8
如果您正在运行 Debian Linux(或衍生产品),并且使用 获得正回报> which psql
,则只需键入psql -V
(大写“V”),您将得到如下回报:psql (PostgreSQL) 9.4.8
回答by vinoy vincent
You may also check in /opt
mount in following path /opt/PostgresPlus/9.5AS/bin/
您也可以/opt
在以下路径中检查安装/opt/PostgresPlus/9.5AS/bin/
回答by user2584621
aptitude show postgresql | grep Version
worked for me
aptitude show postgresql | grep Version
为我工作
回答by johnniepop
For many years I used the command:
多年来,我一直使用命令:
ps aux | grep postgres
On one hand it is useful (for any process) and gives useful info (but from process POV). But on the other hand it is for checking if the server you know, you already installed is running.
一方面,它很有用(对于任何流程)并提供有用的信息(但来自流程 POV)。但另一方面,它用于检查您知道的已安装的服务器是否正在运行。
At some point I found this tutorial, where the usage of the locatecommand is shown. It looks like this command is much more to the point for this case.
在某个时候,我找到了本教程,其中显示了locate命令的用法。看起来这个命令更适合这种情况。