确定PostgreSQL的端口

我知道默认情况下,PostgreSQL监听端口5432,但是实际上确定PostgreSQL端口的命令是什么?

configuration: Ubuntu 9.10与PostgreSQL 8.4

lsof和nmap是解决scheme,但是它们并没有默认安装。 你想要的是netstat(8)。

sudo netstat -plunt |grep postgres 

PostgreSQL实用程序pg_lsclusters显示有关所有群集的configuration和状态(包括端口号)的信息。

 $ pg_lsclusters Version Cluster Port Status Owner Data directory Log file 8.4 main 5433 online postgres /var/lib/postgresql/8.4/main /var/log/postgresql/postgresql-8.4-main.log 

这也有不需要“sudo”权限运行的优点。

在Debian和Ubuntu系统上,pg_lsclusters命令由postgresql-common包提供,默认情况下应该使用postgresql服务器来安装。

如果你想从数据库内部做到这一点,只要做“显示端口”。 但是,假设你已经能够连接到它,至less在本地…

如果您在本地机器上search,我会使用lsof命令来检查postgresql正在使用的端口

 lsof -p <postgres_process_id> 

以下是我find的一个解决scheme:

 sudo apt-get install nmap sudo nmap localhost | grep postgresql 

如果您想要search非本地计算机,只需将localhost更改为服务器的IP地址即可。

我有运行多个postgres实例的机器 – 所以我也有尝试匹配正确的数据库与每个端口的问题。 我倾向于:

 $ ps aux | grep postgres | grep -v 'postgres:' 

然后,对于返回的每个实例,查找目录( -D参数)和:

 $ sudo grep port $DIR/postgresql.conf