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

时间:2022-05-09 21:01:41

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或者没有安装PostgreSQL,我想要检入一个脚本并打印结果。关于如何检查有什么建议吗?

9 个解决方案

#1


50  

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:

如果您要运行哪个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:

看看男人,似乎也有一个选项可以帮助你:

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

因此,似乎只要您使用的任何脚本语言都可以执行一个终端命令,您可以发送哪个-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可执行文件。

Here's the information about using echo $?

这是关于使用echo $的信息?

#2


6  

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.

但是我猜你可以试着用一些类似的标记来启动它,它只是打印一些信息然后退出。

Updated using "service postgres status". Try:

使用“服务postgres状态”更新。试一试:

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

#3


5  

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协议响应。但这一切都不会非常稳健。您可能需要检查您的需求。

#4


4  

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
  • 从源代码安装到/opt或/usr/local,由init脚本手动启动或启动
  • Installed from distributor rpm / deb packages and started via init script
  • 安装在分销商rpm / deb包中,并通过初始化脚本启动。
  • Installed from 3rd party rpm / deb packages and started via init script
  • 安装在第三方rpm / deb包中,并通过init脚本启动。
  • 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.

您不能依赖于路径上的psql。您不能依赖于系统上只有一个psql(多个版本可能以不同的方式安装)。你不能基于端口来做,因为不能保证它在端口5432上,或者没有多个版本。

Prompt the user and ask them.

提示用户并询问他们。

#5


4  

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(或衍生版本),并且您有一个用>进行psql操作的稳定返回,那么只需输入psql -V(大写“V”),您将得到如下的返回:psql (PostgreSQL) 9.4.8

#6


2  

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

给你。

#7


2  

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用户,只需输入$>查找/ -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.

我这样运行,从/ base找到元素所在的整个路径。这将返回包含“postgres”的任何文件或目录。

You could do the same thing looking for the pg_hba.conf or postgresql.conf files also.

你可以用同样的方法来寻找pg_hba。conf或postgresql。conf文件也。

#8


1  

aptitude show postgresql | grep Version worked for me

智能展示postgresql | grep版本对我很有用

#9


0  

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

您也可以在以下路径/opt/PostgresPlus/9.5AS/bin/中检入/选择挂载

#1


50  

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:

如果您要运行哪个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:

看看男人,似乎也有一个选项可以帮助你:

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

因此,似乎只要您使用的任何脚本语言都可以执行一个终端命令,您可以发送哪个-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可执行文件。

Here's the information about using echo $?

这是关于使用echo $的信息?

#2


6  

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.

但是我猜你可以试着用一些类似的标记来启动它,它只是打印一些信息然后退出。

Updated using "service postgres status". Try:

使用“服务postgres状态”更新。试一试:

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

#3


5  

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协议响应。但这一切都不会非常稳健。您可能需要检查您的需求。

#4


4  

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
  • 从源代码安装到/opt或/usr/local,由init脚本手动启动或启动
  • Installed from distributor rpm / deb packages and started via init script
  • 安装在分销商rpm / deb包中,并通过初始化脚本启动。
  • Installed from 3rd party rpm / deb packages and started via init script
  • 安装在第三方rpm / deb包中,并通过init脚本启动。
  • 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.

您不能依赖于路径上的psql。您不能依赖于系统上只有一个psql(多个版本可能以不同的方式安装)。你不能基于端口来做,因为不能保证它在端口5432上,或者没有多个版本。

Prompt the user and ask them.

提示用户并询问他们。

#5


4  

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(或衍生版本),并且您有一个用>进行psql操作的稳定返回,那么只需输入psql -V(大写“V”),您将得到如下的返回:psql (PostgreSQL) 9.4.8

#6


2  

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

给你。

#7


2  

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用户,只需输入$>查找/ -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.

我这样运行,从/ base找到元素所在的整个路径。这将返回包含“postgres”的任何文件或目录。

You could do the same thing looking for the pg_hba.conf or postgresql.conf files also.

你可以用同样的方法来寻找pg_hba。conf或postgresql。conf文件也。

#8


1  

aptitude show postgresql | grep Version worked for me

智能展示postgresql | grep版本对我很有用

#9


0  

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

您也可以在以下路径/opt/PostgresPlus/9.5AS/bin/中检入/选择挂载