使用 PSQL 命令查找主机名和端口

发布于 2024-10-31 03:07:49 字数 65 浏览 7 评论 0原文

我正在运行 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 技术交流群。

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

发布评论

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

评论(20

夕色琉璃 2024-11-07 03:07:49
SELECT *
FROM pg_settings
WHERE name = 'port';
SELECT *
FROM pg_settings
WHERE name = 'port';
不念旧人 2024-11-07 03:07:49

此命令将为您提供 postgres 端口号

 \conninfo

如果 Postgres 在 Linux 服务器上运行,您还可以使用以下命令

sudo netstat -plunt |grep postgres

或(如果它作为 postmaster)

sudo netstat -plunt |grep postmaster

,您将看到类似的内容

tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      140/postgres
tcp6       0      0 ::1:5432                :::*                    LISTEN      140/postgres

在这种情况下,端口号为 5432,这也是默认端口号

信用:链接

This command will give you postgres port number

 \conninfo

If Postgres is running on a Linux server, you can also use the following command

sudo netstat -plunt |grep postgres

OR (if it comes as postmaster)

sudo netstat -plunt |grep postmaster

and you will see something similar as this

tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      140/postgres
tcp6       0      0 ::1:5432                :::*                    LISTEN      140/postgres

In this case, the port number is 5432 which is also the default port number

credit: link

红颜悴 2024-11-07 03:07:49

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

黎歌 2024-11-07 03:07:49

select inet_server_addr(); 为您提供服务器的 IP 地址。

select inet_server_addr(); gives you the ip address of the server.

忆依然 2024-11-07 03:07:49

这是非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.

enter image description here

行至春深 2024-11-07 03:07:49
select inet_server_addr( ), inet_server_port( );
select inet_server_addr( ), inet_server_port( );
夏天碎花小短裙 2024-11-07 03:07:49

select inet_server_port(); 为您提供服务器的端口。

select inet_server_port(); gives you the port of the server.

断念 2024-11-07 03:07:49

从终端,您可以简单地执行“postgres list cluster”:

pg_lsclusters

它将返回 Postgres 版本号、集群名称、端口、状态、所有者以及数据目录和日志文件的位置。

From the terminal you can simply do a "postgres list clusters":

pg_lsclusters

It will return Postgres version number, cluster names, ports, status, owner, and the location of your data directories and log file.

小傻瓜 2024-11-07 03:07:49

postgresql 端口在 postgresql.conf 文件中定义。

对于 Ubuntu 14.04 中的我来说,它是: /etc/postgresql/9.3/main/postgresql.conf

里面有一行:

port = 5432

更改那里的数字需要重新启动 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:

port = 5432

Changing the number there requires restart of postgresql for it to take effect.

星星的轨迹 2024-11-07 03:07:49

从终端您可以执行以下操作:

\conninfo

我建议使用以下命令阅读有关所有命令的详尽列表的文档:

\?

From the terminal you can do:

\conninfo

I would suggest reading a documentation on their exhaustive list of all commands using:

\?

山田美奈子 2024-11-07 03:07:49

您可以在 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".

菊凝晚露 2024-11-07 03:07:49

要查找端口号,您可以运行此命令(假设您位于本地主机)

select setting from pg_settings where name='port';

To find the port number you can run this command (assuming you are on localhost)

select setting from pg_settings where name='port';
能怎样 2024-11-07 03:07:49
SELECT CURRENT_USER usr, :'HOST' host, inet_server_port() port;

这使用 psql 的内置 HOST 变量,记录在此处< /a>

和 postgres 系统信息函数,记录在此处

SELECT CURRENT_USER usr, :'HOST' host, inet_server_port() port;

This uses psql's built in HOST variable, documented here

And postgres System Information Functions, documented here

温柔戏命师 2024-11-07 03:07:49

对@a_horse_with_no_name 答案的补充。
获取主机名:

SELECT boot_val,reset_val FROM pg_settings WHERE name='listen_addresses';;

An addition to the @a_horse_with_no_name answer.
To get the hostname:

SELECT boot_val,reset_val FROM pg_settings WHERE name='listen_addresses';;
墨落成白 2024-11-07 03:07:49
service postgresql status

返回:
10/main(端口5432):在线

我运行的是Ubuntu 18.04

service postgresql status

returns:
10/main (port 5432): online

I'm running Ubuntu 18.04

孤凫 2024-11-07 03:07:49

因为你说你(你自己)正在运行 postgresql,所以我假设:

  • 你在 Linux 上,
  • 至少有一个具有超级用户权限的帐户和/或可以访问 postgres 角色,并且
  • (只是为了好玩)你需要访问这两个帐户来自数据库本身的单个事务中的值

/* SQL CODE */

CREATE TEMP TABLE tmp ( hostname text, port bigint ) ON COMMIT DROP;

COPY tmp FROM PROGRAM $pgm$ printf "$HOSTNAME\t$(i=1 && until [[ "$(psql -U postgres -p $i -qt -c "SELECT 'true'" 2>/dev/null | sed -e '$d' | xargs | tr -d "\n")" == "true" ]]; do i=$(($i+1)) && if [ $i == "65535" ]; then break ; fi ; done && echo $i)"$pgm$ ( format 'text', delimiter '\t' );

SELECT host, port FROM tmp;

将为您提供两者,将 $pgm$ 分隔的代码作为 shell 脚本执行并将值返回到服务器端 COPY API 的标准输入。不幸的是,此方法需要一个表目标来调用服务器端 shell。

如果您需要能够在没有临时表的情况下进行调用,即作为函数调用,请尝试在 中实现上述 shell plsh 语言

Because you said you (yourself) have postgresql running, I'll assume:

  • you're on Linux,
  • have at least one account with superuser privileges and/or can access the postgres role, and
  • (just for fun) you need to access both values within a single transaction from within the database itself

/* SQL CODE */

CREATE TEMP TABLE tmp ( hostname text, port bigint ) ON COMMIT DROP;

COPY tmp FROM PROGRAM $pgm$ printf "$HOSTNAME\t$(i=1 && until [[ "$(psql -U postgres -p $i -qt -c "SELECT 'true'" 2>/dev/null | sed -e '$d' | xargs | tr -d "\n")" == "true" ]]; do i=$(($i+1)) && if [ $i == "65535" ]; then break ; fi ; done && echo $i)"$pgm$ ( format 'text', delimiter '\t' );

SELECT host, port FROM tmp;

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.

信仰 2024-11-07 03:07:49

我认为 PostgreSQL 没有提供内置函数来获取服务器的主机名,因此我们可能需要编写一个扩展来从服务器获取信息。

我发现有一个 PostgreSQL 扩展 pg-hostname 可以从服务器获取主机名。

当我们安装了扩展程序后,我们可以启用它并通过 inet_server_port 和查询信息。 主机名函数。

CREATE EXTENSION hostname;

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

CREATE EXTENSION hostname;

SELECT hostname(),inet_server_port();
始终不够爱げ你 2024-11-07 03:07:49

我使用这样的解决方案。无需插件。不需要临时表。仅适用于 UNIX。

select pg_read_file('/etc/hostname') as hostname, setting as port from pg_settings where name='port';

I use such a solution. No plugins required. Temporary tables are not needed. Only for unix.

select pg_read_file('/etc/hostname') as hostname, setting as port from pg_settings where name='port';
我不会写诗 2024-11-07 03:07:49

右键单击您的 SQL 服务器。我的是 PostgresSQL 13 并选择属性 ->联系。它有:

  • 主机名
  • 端口ID
  • 用户名。

Right click your SQL server. Mine is PostgresSQL 13 and select properties -> connection. This has:

  • host name
  • port ID
  • Username.
遥远的绿洲 2024-11-07 03:07:49

转到“终端”,只需

service postgres status

在结果中输入即可获取端口详细信息运行 postgres 详细信息

在我的例子中,它在端口“5432”(默认)上运行。
I我正在使用 CentOS 7。希望这有帮助。

go to the "Terminal" and just type

service postgres status

In the results you can get the port detailsRunning postgres details

In my case it's running on port "5432" (default).
I'm using CentOS 7.Hope this helps.

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