Tambena Consulting

How to Tell What Port Postgres is Running on?

PostgreSQL is a flexible relational database that is used in various environments. It can be used in local development to complex production systems. However, identifying the port on which PostgreSQL is running is important especially if multiple data instances, network configuration, and troubleshooting issues are involved. 

By default, PostgreSQL listens on port 5432, but this can be customized based on specific needs. This guide provides detailed guidance on how to tell what port Postgres is running on across different platforms, configurations, and tools.

Understanding PostgreSQL’s Default Port and Configurability

By default, PostgreSQL is set to listen on port 5432. This port is configured in the `postgresql.conf` file, which serves as the main configuration file for PostgreSQL’s settings. When running multiple PostgreSQL instances on a single machine, each instance must be assigned a unique port to avoid conflicts. Here’s how to check for the port using the `postgresql.conf` file and several other methods depending on your access and operating system.

Finding the Port in the `postgresql.conf` File

The `postgresql.conf` file is the first place to check when you need to find PostgreSQL ports. It’s located in the data directory of your PostgreSQL installation, which varies by operating system and installation method.

Locate the Configuration File

Linux (Debian/Ubuntu): `/etc/postgresql/<version>/main/postgresql.conf`

Linux (RedHat/CentOS): `/var/lib/pgsql/data/postgresql.conf`

Windows: `C:\Program Files\PostgreSQL\<version>\data\postgresql.conf`

macOS (Homebrew): `/usr/local/var/postgres/postgresql.conf`

Open the Configuration File

   Open `postgresql.conf` in a text editor. On Linux, you might use:

   “`bash

   sudo nano /etc/postgresql/<version>/main/postgresql.conf

   “`

Locate the Port Setting

Within `postgresql.conf`, look for the following line:

   “`plaintext

   port = 5432

   “`

If this line is commented out (preceded by a `#`), PostgreSQL is typically defaulting to port 5432 unless overridden.

This file allows you to change the port by simply editing the value and restarting PostgreSQL.

Checking the Port via SQL Query with `psql`

If you’re able to connect to PostgreSQL using `psql` or another SQL client, you can check the current port setting directly with a SQL query.

Connect to PostgreSQL with `psql`

   “`bash

   psql -U postgres -d your_database_name

   “`

Run a Query to Check the Port

   Once connected, execute:

   “`sql

   SHOW port;

   “`

PostgreSQL will return the port number used by the current instance. This method is especially useful for remote connections where you can’t directly access configuration files.

Using Network Utilities like `netstat` or `ss` to Find the Port

If PostgreSQL is running, you can use network utility commands like `netstat` or `ss` to identify the port.

Using `netstat`

The `netstat` command lists active network connections, including the ports they are listening on.

“`bash

sudo netstat -plnt | grep postgres

“`

`-p` shows the process name, `-l` lists listening ports, `-n` shows numerical addresses, and `-t` lists TCP connections.

The output will show lines where PostgreSQL is listening. Look for an entry similar to `127.0.0.1:5432`, indicating the IP and port.

Using `ss` (Linux)

`ss` is a modern replacement for `netstat` and offers a similar way to check listening ports.

“`bash

sudo ss -plnt | grep postgres

“`

This command should return a line like `127.0.0.1:5432` or similar if PostgreSQL is listening on port 5432.

Checking the Port in the `pg_hba.conf` File

The `pg_hba.conf` file configures authentication and can also provide clues about network configurations. While it doesn’t directly define the port, it includes IP addresses and authentication methods that can confirm which connections PostgreSQL will accept.

Locate and Open `pg_hba.conf`

   “`bash

   sudo nano /etc/postgresql/<version>/main/pg_hba.conf

   “`

Examine the `host` Entries

Entries with `host` or `hostssl` typically represent remote access. This can help confirm which IP configurations PostgreSQL is using for connections, although the port is defined elsewhere.

Using Service Management Commands

PostgreSQL is often managed as a service, especially in production environments. This method allows you to query the service status and sometimes obtain port details.

Using `systemctl` on Linux

For systems using `systemd`, you can check PostgreSQL’s status as follows:

“`bash

sudo systemctl status postgresql

“`

This command will provide information on whether PostgreSQL is active, which configuration it’s using, and sometimes the port.

Using Homebrew on macOS

If you installed PostgreSQL using Homebrew on macOS, you can check the status with:

“`bash

brew services list | grep postgres

“`

Windows Services

In Windows, open the Services application by typing `services.msc` in the Run dialog. Locate PostgreSQL in the list, right-click, and check Properties. Although this does not directly display the port, it shows the status and additional configuration details, which can help in locating the port.

Environment Variables

Some PostgreSQL instances are configured to use environment variables for port settings, particularly in containerized or cloud environments.

Check Environment Variables on Linux/macOS

   “`bash

   env | grep PGPORT

   “`

 If the `PGPORT` variable is set, it will display the port value. This is common in environments where PostgreSQL instances are dynamically configured at runtime.

Check Environment Variables on Windows

Open Command Prompt or PowerShell and use:

     “`powershell

     echo %PGPORT%

     “`

 If `PGPORT` is set, this command will print the port.

Analyzing Application Logs

PostgreSQL logs contain startup messages that often include the port. Checking these logs can reveal the port, particularly if other methods are inaccessible.

Locate PostgreSQL Logs

On Linux, PostgreSQL logs are often in `/var/log/postgresql/`.

On Windows, look in the `pg_log` directory within the PostgreSQL installation directory.

Search the Logs for Port Information

 Open the log file and look for lines like:

   “`plaintext

   LOG:  database system is ready to accept connections on port 5432

   “`

These messages are logged each time PostgreSQL starts and confirms the active port.

Automating Port Detection with a Script

If you frequently need to check PostgreSQL’s port, consider using a script to automate this. Here’s an example of a Bash script for Linux/macOS:

“`bash

#!/bin/bash

PORT=$(psql -U postgres -tAc “SHOW port”)

if [ -n “$PORT” ]; then

  echo “PostgreSQL is running on port: $PORT”

else

  echo “Unable to detect PostgreSQL port.”

fi

“`

This script attempts to connect to PostgreSQL and retrieve the port setting, making it a quick way to identify the port in a controlled environment.

Conclusion

How to tell what port Postgres is running on is critical for managing database access, troubleshooting connection issues, and configuring network security. By checking `PostgreSQL.conf`, using network tools, exploring service configurations, and analyzing logs, you can reliably identify the port on which PostgreSQL is listening. Following these steps ensures that you can manage PostgreSQL efficiently, whether in local development or complex production environments.

Aneeb Ahmad

Aneeb Ahmad

Aneeb is a full-stack SEO & Content Marketer. He drives our inbound marketing efforts on all touchpoints & writes just about everything under the sun! He loves talking about football when he’s not wordsmithing. Email: aneebahmad1@gmail.com

Get A Free Qoute

Please enable JavaScript in your browser to complete this form.