如何通过 Linux 脚本检查 PostgreSQL 是否已安装?
我想检查 Linux 上是否安装了 PostgreSQL 并打印结果。 关于如何进行检查有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
没有直接的方法可以做到这一点。您所能做的就是检查软件包管理器(rpm、dpkg)或探测您想要的文件的一些可能位置。或者您可以尝试连接到可能的端口 (5432),看看是否收到 PostgreSQL 协议响应。但这些都不会非常稳健。您可能需要检查您的要求。
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.
如果您正在运行 Debian Linux(或衍生版本)并且您有一个带有
> 的正返回值,其中 psql
,然后只需输入psql -V
(大写“V”),您将得到如下返回:psql (PostgreSQL) 9.4.8
If you are running Debian Linux (or derivative) and if you have a postive return with
> which psql
, then simply typepsql -V
(capital "V") and you will get a return like:psql (PostgreSQL) 9.4.8
如果这些绝佳答案的选择都失败了,您始终可以像这样使用“查找”。或者您可能需要使用 sudo
如果您是 root,只需输入
$$>; find / -name 'postgres'
如果您是用户,则需要 sudo priv 才能在所有目录中运行它
,我以这种方式运行它,从
/
库查找整个目录找到该元素的路径。这将返回其中包含“postgres”的任何文件或目录。您也可以执行相同的操作来查找 pg_hba.conf 或 postgresql.conf 文件。
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
If you are root, just type
$$> find / -name 'postgres'
If you are a user, you will need sudo priv's to run it through all the directories
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.You could do the same thing looking for the
pg_hba.conf
orpostgresql.conf
files also.多年来我一直使用该命令:
一方面它很有用(对于任何进程)并且提供有用的信息(但来自进程 POV)。但另一方面,它是为了检查您知道的、已安装的服务器是否正在运行。
在某个时候,我发现本教程,其中显示了 locate 命令的用法。看起来这个命令对于这种情况来说更有意义。
For many years I used the command:
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.
At some point I found this tutorial, where the usage of the locate command is shown. It looks like this command is much more to the point for this case.
aptitude 显示 postgresql | grep Version 对我有用
aptitude show postgresql | grep Version
worked for medpkg -l | dpkg -l | grep postgres
运行上述命令,如果您得到如图所示的输出,则它在您的系统上不存在
来源:-点击此处了解更多信息
dpkg -l | grep postgres
RUN the above command and if you get the output as shown in the image then it does not exist on your system
Source:-Click here for more info
转到 postgres db 的 bin 目录,例如
/opt/postgresql/bin
&运行以下命令:给你。 。
Go to bin directory of postgres db such as
/opt/postgresql/bin
& run below command :Here you go . .
您还可以在以下路径中签入
/opt
挂载/opt/PostgresPlus/9.5AS/bin/
You may also check in
/opt
mount in following path/opt/PostgresPlus/9.5AS/bin/
嗯,上面的所有答案都很好,但并非在所有情况下都如此。
基本上检查文件夹
/etc/postgresql/
在大多数情况下会有一个子文件夹,例如。
/etc/postgresql/11/
(或/etc/postgresql/12
) 这意味着您已经安装了 11(或 12)版本,但是在许多情况下您可能有许多这样的子文件夹,因此拥有它们意味着所有这些版本都已安装并且可以使用......所以请注意这个重要的跟踪。ps使用Ubuntu 18.04
Well, all answersabove are good but not in all cases.
Basically check the folder
/etc/postgresql/
in most cases there will be one subfolder eg.
/etc/postgresql/11/
(or/etc/postgresql/12
) which means that you have installed 11 (or 12) version, however in many cases you may have many of such subfolders, so having them all means that all those versions had been ever installed and could be in use ... so be aware of this important trace.ps using Ubuntu 18.04
前往窗口服务广告搜索 PostGreSQL。
在这里我发现
Go window services ad search for PostGreSQL.
here I found
警告:这只是检查是否安装了 postgres CLIENT(可以在不安装服务器的情况下安装)
AI 建议这样做:
可能 ubuntu 唯一的方法是使用二进制文件的绝对路径:
但它非常令人困惑的是,这是如此棘手,而且 postgres 开发人员没有记录这个用例?
在安装过程中,应将软链接从 /usr/lib/postgresql/14/bin/postgres 放置到 /usr/bin ,
该链接指向安装的(最新?)postgres 二进制文件,因此一个简单的 postgres --version 可以工作并显示如果安装了 postgres SERVER。
WARNING: this is only checking if postgres CLIENT is installed (which can be installed WITHOUT installing the server)
AI suggested this:
possibly a ubuntu only way is to use absolute path to the binary:
but it is very bewildering that this is so tricky and that this use case was not documented by postgres developers?
during install a soft link should be put from under /usr/lib/postgresql/14/bin/postgres to /usr/bin
that points to the (lastest?) postgres binary installed, so a simple postgres --version would work and show if postgres SERVER is installed.
尝试一下
which
命令怎么样?如果您要运行
which psql
并且未安装 Postgres,则似乎没有输出。您只需让终端提示符准备好接受另一个命令:但是如果安装了 Postgres,您将收到包含 Postgres 安装位置路径的响应:
查看
man which
似乎还有一个可以帮助您的选项:因此,似乎只要您使用的任何脚本语言都可以执行终端命令,您就可以发送
which -s psql
并使用返回值来确定是否Postgres 已安装。从那里您可以随心所欲地打印结果。我的机器上确实安装了 postgres,因此我运行以下命令
,它告诉我该命令返回 0,表明在我的机器上找到了 Postgres 可执行文件。
以下是有关使用
的信息回显$?
What about trying the
which
command?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:But if Postgres is installed you'll get a response with the path to the location of the Postgres install:
Looking at
man which
there also appears to be an option that could help you out: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.I do have postgres installed on my machine so I run the following
which tells me that the command returned 0, indicating that the Postgres executable was found on my machine.
Here's the information about using
echo $?
我们可以简单地写:
输出显示如下:
We can simply write:
output show like:
如果是基于 Debian 的。
但我想你可以尝试使用像
--version
这样的标志来启动它,它只是打印一些信息并退出。您可以检查该服务是否可用。尝试
在更广泛的 Linux 发行版中使用
systemctl status postgresql
即可。If it is debian based.
But I guess you can just try to launch it with some flag like
--version
, that simply prints some info and exits.You can check if the service is available. Try
or in a broader range of Linux distros
systemctl status postgresql
can be used.没有单一简单的方法可以做到这一点,因为 PostgreSQL 可能以多种不同的方式安装和设置:
/opt
或/usr /local
,手动启动或由 init 脚本启动rpm
/deb
软件包安装,并通过 init 脚本rpm< 安装/代码> / <代码>deb软件包并通过 init 脚本启动
PATH
或默认路径上port您不能依赖
psql
位于PATH
上。您不能依赖系统上只有一个psql
(可能会以不同的方式安装多个版本)。您无法基于端口执行此操作,因为无法保证它位于端口 5432 上,或者不保证不存在多个版本。提示用户并询问他们。
There is no single simple way to do it, because PostgreSQL might be installed and set up in many different ways:
/opt
or/usr/local
, manually started or started by an init scriptrpm
/deb
packages and started via init scriptrpm
/deb
packages and started via init scriptPATH
or default portYou can't rely on
psql
being on thePATH
. You can't rely on there being only onepsql
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.Prompt the user and ask them.