使用 PSQL 命令查找主机名和端口
我正在运行 PSQL,并且正在尝试让 Perl 应用程序连接到数据库。是否有命令可以查找数据库当前运行的端口和主机?
I have PSQL running, and am trying to get a perl application connecting to the database. Is there a command to find the current port and host that the database is running on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
此命令将为您提供 postgres 端口号
如果 Postgres 在 Linux 服务器上运行,您还可以使用以下命令
或(如果它作为 postmaster)
,您将看到类似的内容
在这种情况下,端口号为 5432,这也是默认端口号
信用:链接
This command will give you postgres port number
If Postgres is running on a Linux server, you can also use the following command
OR (if it comes as postmaster)
and you will see something similar as this
In this case, the port number is 5432 which is also the default port number
credit: link
默认 PostgreSQL 端口为
5432
。数据库运行的主机应该由您的托管提供商提供;我猜想如果没有指定的话,它将与网络服务器是同一台主机。通常,这将配置为 localhost,假设您的 Web 服务器和数据库服务器位于同一主机上。The default PostgreSQL port is
5432
. The host that the database is operating on should have been provided by your hosting provider; I'd guess it would be the same host as the web server if one wasn't specified. Typically this would be configured as localhost, assuming your web server and database server are on the same host.select inet_server_addr();
为您提供服务器的 IP 地址。select inet_server_addr();
gives you the ip address of the server.这是非sql方法。图像本身给出了说明。选择您要查找其信息的服务器,然后按照步骤操作。
This is non-sql method. Instructions are given on the image itself. Select the server that you want to find the info about and then follow the steps.
select inet_server_port();
为您提供服务器的端口。select inet_server_port();
gives you the port of the server.从终端,您可以简单地执行“postgres list cluster”:
它将返回 Postgres 版本号、集群名称、端口、状态、所有者以及数据目录和日志文件的位置。
From the terminal you can simply do a "postgres list clusters":
It will return Postgres version number, cluster names, ports, status, owner, and the location of your data directories and log file.
postgresql 端口在
postgresql.conf
文件中定义。对于 Ubuntu 14.04 中的我来说,它是:
/etc/postgresql/9.3/main/postgresql.conf
里面有一行:
更改那里的数字需要重新启动 postgresql 才能生效。
The postgresql port is defined in your
postgresql.conf
file.For me in Ubuntu 14.04 it is:
/etc/postgresql/9.3/main/postgresql.conf
Inside there is a line:
Changing the number there requires restart of postgresql for it to take effect.
从终端您可以执行以下操作:
\conninfo
我建议使用以下命令阅读有关所有命令的详尽列表的文档:
\?
From the terminal you can do:
\conninfo
I would suggest reading a documentation on their exhaustive list of all commands using:
\?
您可以在 psql
\conninfo
中使用该命令您将得到
您已以用户“user_name”的身份连接到主机“host_name”的端口“port_number”上的数据库“your_database”。
You can use the command in psql
\conninfo
you will get
You are connected to database "your_database" as user "user_name" on host "host_name" at port "port_number".
要查找端口号,您可以运行此命令(假设您位于本地主机)
To find the port number you can run this command (assuming you are on localhost)
这使用 psql 的内置 HOST 变量,记录在此处< /a>
和 postgres 系统信息函数,记录在此处
This uses psql's built in HOST variable, documented here
And postgres System Information Functions, documented here
对@a_horse_with_no_name 答案的补充。
获取主机名:
An addition to the @a_horse_with_no_name answer.
To get the hostname:
返回:
10/main(端口5432):在线
我运行的是Ubuntu 18.04
returns:
10/main (port 5432): online
I'm running Ubuntu 18.04
因为你说你(你自己)正在运行 postgresql,所以我假设:
/* SQL CODE */
将为您提供两者,将 $pgm$ 分隔的代码作为 shell 脚本执行并将值返回到服务器端 COPY API 的标准输入。不幸的是,此方法需要一个表目标来调用服务器端 shell。
如果您需要能够在没有临时表的情况下进行调用,即作为函数调用,请尝试在 中实现上述 shell plsh 语言。
Because you said you (yourself) have postgresql running, I'll assume:
/* SQL CODE */
will give you both, executing the $pgm$-delimited code as a shell script and returning the values to the server-side COPY API's stdin. Unfortunately, this method needs a table target to invoke the server-side shell.
If you need to be able to call without a temp table, i.e. as a function invocation, try implementing the above shell in the plsh language.
我认为 PostgreSQL 没有提供内置函数来获取服务器的主机名,因此我们可能需要编写一个扩展来从服务器获取信息。
我发现有一个 PostgreSQL 扩展 pg-hostname 可以从服务器获取主机名。
当我们安装了扩展程序后,我们可以启用它并通过 inet_server_port 和查询信息。
主机名
函数。I think PostgreSQL didn't provide an in-built function to get the hostname of server so we might need to write an extension to get the information from server.
I found there is a PostgreSQL extension pg-hostname which can get the hostname from server.
When we have installed the extension we can enable that and query information by
inet_server_port
&hostname
function.我使用这样的解决方案。无需插件。不需要临时表。仅适用于 UNIX。
I use such a solution. No plugins required. Temporary tables are not needed. Only for unix.
右键单击您的 SQL 服务器。我的是 PostgresSQL 13 并选择属性 ->联系。它有:
Right click your SQL server. Mine is PostgresSQL 13 and select properties -> connection. This has:
转到“终端”,只需
在结果中输入即可获取端口详细信息
在我的例子中,它在端口“5432”(默认)上运行。
I我正在使用 CentOS 7。希望这有帮助。
go to the "Terminal" and just type
In the results you can get the port details
In my case it's running on port "5432" (default).
I'm using CentOS 7.Hope this helps.