如何通过 Linux 脚本检查 PostgreSQL 是否已安装?

发布于 2024-11-03 18:04:47 字数 59 浏览 0 评论 0原文

我想检查 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

聆听风音 2024-11-10 18:04:48

没有直接的方法可以做到这一点。您所能做的就是检查软件包管理器(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.

Saygoodbye 2024-11-10 18:04:48

如果您正在运行 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 type psql -V (capital "V") and you will get a return like: psql (PostgreSQL) 9.4.8

揽月 2024-11-10 18:04:48

如果这些绝佳答案的选择都失败了,您始终可以像这样使用“查找”。或者您可能需要使用 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 or postgresql.conf files also.

野心澎湃 2024-11-10 18:04:48

多年来我一直使用该命令:

ps aux | grep postgres

一方面它很有用(对于任何进程)并且提供有用的信息(但来自进程 POV)。但另一方面,它是为了检查您知道的、已安装的服务器是否正在运行。

在某个时候,我发现本教程,其中显示了 locate 命令的用法。看起来这个命令对于这种情况来说更有意义。

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.

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.

作业与我同在 2024-11-10 18:04:48

aptitude 显示 postgresql | grep Version 对我有用

aptitude show postgresql | grep Version worked for me

摇划花蜜的午后 2024-11-10 18:04:48

dpkg -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

Output of the command

Source:-Click here for more info

enter image description here

转到 postgres db 的 bin 目录,例如 /opt/postgresql/bin &运行以下命令:

[...bin]# ./psql --version

psql (PostgreSQL) 9.0.4

给你。 。

Go to bin directory of postgres db such as /opt/postgresql/bin & run below command :

[...bin]# ./psql --version

psql (PostgreSQL) 9.0.4

Here you go . .

面犯桃花 2024-11-10 18:04:48

您还可以在以下路径中签入 /opt 挂载 /opt/PostgresPlus/9.5AS/bin/

You may also check in /opt mount in following path /opt/PostgresPlus/9.5AS/bin/

黄昏下泛黄的笔记 2024-11-10 18:04:48

嗯,上面的所有答案都很好,但并非在所有情况下都如此。

基本上检查文件夹 /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

滴情不沾 2024-11-10 18:04:48

前往窗口服务广告搜索 PostGreSQL。
在这里我发现
输入图片此处描述

Go window services ad search for PostGreSQL.
here I found
enter image description here

深居我梦 2024-11-10 18:04:48

警告:这只是检查是否安装了 postgres CLIENT(可以在不安装服务器的情况下安装)

psql --version; 

AI 建议这样做:

pg_isready 
/var/run/postgresql:5432 - accepting connections

可能 ubuntu 唯一的方法是使用二进制文件的绝对路径:

/usr/lib/postgresql/14/bin/postgres --version
postgres (PostgreSQL) 14.13 (Ubuntu 14.13-1.pgdg22.04+1)

/usr/lib/postgresql/12/bin/postgres --version
postgres (PostgreSQL) 12.20 (Ubuntu 12.20-1.pgdg22.04+1)

但它非常令人困惑的是,这是如此棘手,而且 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)

psql --version; 

AI suggested this:

pg_isready 
/var/run/postgresql:5432 - accepting connections

possibly a ubuntu only way is to use absolute path to the binary:

/usr/lib/postgresql/14/bin/postgres --version
postgres (PostgreSQL) 14.13 (Ubuntu 14.13-1.pgdg22.04+1)

/usr/lib/postgresql/12/bin/postgres --version
postgres (PostgreSQL) 12.20 (Ubuntu 12.20-1.pgdg22.04+1)

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.

彼岸花似海 2024-11-10 18:04:47

尝试一下 which 命令怎么样?

如果您要运行 which psql 并且未安装 Postgres,则似乎没有输出。您只需让终端提示符准备好接受另一个命令:

> which psql
>

但是如果安装了 Postgres,您将收到包含 Postgres 安装位置路径的响应:

> which psql
/opt/boxen/homebrew/bin/psql

查看 man which 似乎还有一个可以帮助您的选项:

-s      No output, just return 0 if any of the executables are found, or
        1 if none are found.

因此,似乎只要您使用的任何脚本语言都可以执行终端命令,您就可以发送 which -s psql 并使用返回值来确定是否Postgres 已安装。从那里您可以随心所欲地打印结果。

我的机器上确实安装了 postgres,因此我运行以下命令

> which -s psql
> echo $?
0

,它告诉我该命令返回 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:

> which psql
>

But if Postgres is installed you'll get a response with the path to the location of the Postgres install:

> which psql
/opt/boxen/homebrew/bin/psql

Looking at man which there also appears to be an option that could help you out:

-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.

I do have postgres installed on my machine so I run the following

> which -s psql
> echo $?
0

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 $?

好菇凉咱不稀罕他 2024-11-10 18:04:47

我们可以简单地写:

psql --version

输出显示如下:

psql (PostgreSQL) 11.5 (Ubuntu 11.5-1.pgdg18.04+1)

We can simply write:

psql --version

output show like:

psql (PostgreSQL) 11.5 (Ubuntu 11.5-1.pgdg18.04+1)
若言繁花未落 2024-11-10 18:04:47

如果是基于 Debian 的。

aptitude show postgresql | grep State

但我想你可以尝试使用像 --version 这样的标志来启动它,它只是打印一些信息并退出。

您可以检查该服务是否可用。尝试

service postgresql status
if [ "$?" -gt "0" ]; then
  echo "Not installed".
else
  echo "Intalled"
fi

在更广泛的 Linux 发行版中使用 systemctl status postgresql 即可。

If it is debian based.

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.

You can check if the service is available. Try

service postgresql status
if [ "$?" -gt "0" ]; then
  echo "Not installed".
else
  echo "Intalled"
fi

or in a broader range of Linux distros systemctl status postgresql can be used.

妄司 2024-11-10 18:04:47

没有单一简单的方法可以做到这一点,因为 PostgreSQL 可能以多种不同的方式安装和设置:

  • 从用户主目录中的源代码安装
  • 从源代码安装到 /opt/usr /local,手动启动或由 init 脚本启动
  • 从发行商 rpm / deb 软件包安装,并通过 init 脚本
  • 启动 从第 3 方 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:

  • 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 not set to start
  • Client installed, connecting to a server on a different computer
  • Installed and running but not on the default PATH or default port

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.

Prompt the user and ask them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文