Table of Contents
- Which Types of Applications Need Ports to Operate?
- Why Use a Non-Default Port for PostgreSQL?
- Identifying the PostgreSQL Port on Linux
- Identifying the PostgreSQL Port on Windows
- Identifying the PostgreSQL Port on MacOS
- Identifying the PostgreSQL Port on Timescale Cloud
- Conclusion
Finding your PostgreSQL port might seem like a simple task, but it can sometimes feel like searching for a needle in a haystack of configuration files and system settings.
The challenge often stems from the fact that PostgreSQL can be installed in various ways (package managers, manual installation, Docker containers), and each method might set different default ports or store configuration information in different locations. Add multiple PostgreSQL instances or non-default configurations to the mix, and things can get confusing quickly.
In today’s article, I will try to answer some frequently asked questions about:
- The real need for system ports
- Why use a non-default port for PostgreSQL?
Next, we will explore various methods to determine the port PostgreSQL is running on across different platforms, including Linux, Windows, and macOS.
Let's get started!
Understanding the Real Need for System Ports
In a network, the system port number plays a crucial role in ensuring that your application connects to the right service on a device. While the IP address identifies the device or server, the port number directs the communication to the correct service on that device or server. Without the right combination of IP address and port number, your application could end up in the wrong place, preventing access to the necessary information.
Ports are indeed necessary for network applications—they're like specific doors or endpoints that applications use to communicate over a network. Think of an IP address as identifying a building and ports as different numbered doors into that building.
The most abstract level where systems typically work with ports is at the Transport Layer (Layer 4) of the OSI (open systems interconnection) model, specifically with protocols like TCP (transmission control protocol) and UDP (user datagram protocol).
Let's break down the concepts of host and port with a simple example. Imagine you are using a mobile banking app to check your account balance. Here’s how the communication happens behind the scenes:
- When you log in to your banking app, it needs to connect to the bank’s server to retrieve your account details. However, the app doesn’t directly know the server's IP address. It queries a DNS (domain name system) to resolve the server's hostname (e.g., api.mybank.com) into its corresponding IP address.
- Once the app has the server's IP address, it uses this to establish a connection. However, the IP address alone isn’t sufficient. It also needs to know the correct port to send requests to. Ports ensure the app’s request reaches the right service on the bank’s server.
- The bank’s server might have an API running on port 443 for secure HTTPS traffic. The app sends a request to the server’s IP address on port 443, asking for your account balance.
- The bank’s server listens for incoming HTTPS requests on port 443, processes the authentication and request data, retrieves your account balance, and sends the response back to the app.
- Once the app receives the response, it displays your account balance on the screen, showing you the requested information.
The final address would look something like this: <IP_ADDRESS:PORT_NUMBER>
(e.g., 203.0.113.10:443).
Here, the IP address identifies the bank’s server, while the port number (443) ensures the request is directed to the secure HTTPS API service. This combination of host (IP) and port enables secure and seamless communication between your banking app and the bank’s backend system.
Which Types of Applications Need Ports to Operate?
Applications that need to communicate over a network typically require ports to operate. Ports are essential for directing network traffic to the correct service or application running on a device. The following types of applications require ports to operate.
IoT (Internet of Things) devices
IoT devices often use ports for communication with central hubs or servers, such as port 1883 for MQTT (Message Queuing Telemetry Transport).
Web servers (HTTP/HTTPS)
Web servers like Apache, Nginx, and IIS use port 80 for HTTP and port 443 for HTTPS to handle incoming web traffic.
Database servers
Applications like PostgreSQL, MySQL, and MongoDB require specific ports to allow remote connections. For instance, PostgreSQL uses port 5432, while MySQL uses port 3306.
Email servers
Email protocols use specific ports to handle communication between email clients and servers. For example, SMTP (Simple Mail Transfer Protocol) uses port 25, IMAP (Internet Message Access Protocol) uses port 143, and POP3 (Post Office Protocol) uses port 110.
File transfer protocol (FTP)
FTP servers require port 21 to facilitate file transfers between a client and a server.
Remote desktop applications
Applications like RDP (remote desktop protocol) use port 3389 to allow remote access to computers or servers.
Now that we have developed a better understanding of what a system port is and its role in network communication, let's explore why it might be necessary to change the default PostgreSQL port (5432).
Why Use a Non-Default Port for PostgreSQL?
Running PostgreSQL on a non-default port can have several advantages—as seen in our previous examples. Here are some of the benefits:
Enhanced security by hiding the default port
By changing the default port (5432), you add an additional layer of security. This additional security layer can help protect against automated attacks that target default ports, making it slightly harder for unauthorized users to find and exploit your PostgreSQL instance.
Avoiding port conflicts
If multiple PostgreSQL instances are running on the same server, changing the port can prevent conflicts and ensure the smooth operation of all services.
Network segmentation and compliance
Running PostgreSQL on a different port can support network segmentation strategies, isolating services for security and performance purposes. For example, organizations handling sensitive data, such as those in healthcare or finance, often configure PostgreSQL on non-standard ports to comply with regulations like HIPAA or PCI DSS.
These regulations may require enhanced measures to restrict unauthorized access and ensure critical services are less predictable to potential attackers. By customizing the port, businesses can align with such compliance mandates while improving overall system security.
Identifying the PostgreSQL Port on Linux
There are several methods to find the port PostgreSQL is using on a Linux system. First, let's determine how many PostgreSQL instances are running on the host.
Command
ps -ef | grep -i postgres
Output
postgres 3785 1 0 09:49 ? 00:00:00 /usr/lib/postgresql/14/bin/postgres-D /var/lib/postgresql/14/main -c config_file=/etc/postgresql/14/main/postgresql.conf
postgres 6316 1 0 09:55 ? 00:00:00 /usr/lib/postgresql/15/bin/postgres -D /var/lib/postgresql/15/main -c config_file=/etc/postgresql/15/main/postgresql.conf
postgres 7205 1 0 09:55 ? 00:00:00 /usr/lib/postgresql/17/bin/postgres -D /var/lib/postgresql/17/main -c config_file=/etc/postgresql/17/main/postgresql.conf
From the above output, we can get the following information:
- PostgreSQL 14
- Installation directory = /usr/lib/postgresql/14/bin
- Configuration file = /etc/postgresql/14/main/postgresql.conf
- Process ID = 3785
- PostgreSQL 15
- Installation directory = /usr/lib/postgresql/15/bin
- Configuration file = /etc/postgresql/14/main/postgresql.conf
- Process ID = 6316
- PostgreSQL 17
- Installation directory = /usr/lib/postgresql/17/bin
- Configuration file = /etc/postgresql/14/main/postgresql.conf
- Process ID = 7205
So, in short, we have three PostgreSQL instances running on the host.
The next step is to identify the port on which each PostgreSQL instance is running.
Command
sudo netstat -plnt | grep postgres
Output
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 3785/postgres
tcp 0 0 127.0.0.1:5433 0.0.0.0:* LISTEN 6316/postgres
tcp 0 0 127.0.0.1:5434 0.0.0.0:* LISTEN 7205/postgres
Now, we can map the process ID from the netstat
command to the output of the previous ps command to retrieve the following details.
- Process 3785 (PostgreSQL 14) is running on port 5432.
- Process 6316 (PostgreSQL 15) is running on port 5433.
- Process 7205 (PostgreSQL 17) is running on port 5434.
Simple, right? Easy to follow!
Make sure that the net-tools package is installed on your system first by running:
sudo apt install net-tools -y
This will ensure that you have the necessary tools (like netstat
) available to check the ports.
Identifying the ports without internet
Let's assume you don't have netstat
installed on your system, and you also don't have internet access to install the package. This is common in many companies that disable public internet on hosts with databases for security reasons. In this case, you can check the postgresql.conf file to find out the ports.
From the above ps
command, we already have the path of the PostgreSQL configuration file:
- /etc/postgresql/14/main/postgresql.conf
- /etc/postgresql/15/main/postgresql.conf
- /etc/postgresql/17/main/postgresql.conf
You can easily determine the ports for each PostgreSQL instance by checking the postgresql.conf file. Use the following commands to identify the port numbers:
cat /etc/postgresql/14/main/postgresql.conf | grep "port\ ="port = 5432 # (change requires restart)
cat /etc/postgresql/15/main/postgresql.conf | grep "port\ ="port = 5433 # (change requires restart)
cat /etc/postgresql/17/main/postgresql.conf | grep "port\ ="port = 5434 # (change requires restart)
From this, we can see that PostgreSQL 14 is using the default port 5432, while PostgreSQL 15 is using 5433, and PostgreSQL 17 is using 5434.
Identifying the port from log files
Let's assume that the PostgreSQL port was manually configured using the pg_ctl command, and there is no information available about the port in the postgresql.conf file. In that case, the port variable in the postgresql.conf file might appear as:
#Port = 5432
In such cases, you can find the manually configured port by checking the log files. Here are the commands you can run to determine the port:
cat /var/log/postgresql/postgresql-14-main.log | grep port2024-12-15 09:49:59.478 UTC [3785] LOG: listening on IPv4 address "127.0.0.1", port 5432
cat /var/log/postgresql/postgresql-15-main.log | grep port2024-12-15 09:55:41.046 UTC [6316] LOG: listening on IPv4 address "127.0.0.1", port 5433
cat /var/log/postgresql/postgresql-17-main.log | grep port2024-12-15 09:55:45.495 UTC [7205] LOG: listening on IPv4 address "127.0.0.1", port 5434
By default, PostgreSQL log files are located in the /var/log/postgresql
directory.
Identifying the PostgreSQL Port on Windows
On Windows, there are several methods you can use to retrieve PostgreSQL port information. Here are the details:
Identifying the port with the installation summary log file
On Windows, the default installation directory for PostgreSQL is C:\Program Files\PostgreSQL
. Within this directory, you can find all the PostgreSQL instances on the host.
For example, in the image below, you can see that I have two PostgreSQL servers installed: versions 14 and 17.
If we navigate to the 14 directory, we can find a file named installation_summary.log
.
Inside this file, you can see the port on which PostgreSQL is running. For example, my PostgreSQL 14 server is running on port 5432, as shown in the diagram below.
Similarly, PostgreSQL 17 is running on port 5433.
Identifying the port via PowerShell
Another option is to use PowerShell. Open PowerShell and run the following command:
Get-NetTCPConnection | Select-Object LocalPort, OwningProcess | ForEach-Object { $_ | Add-Member -MemberType NoteProperty -Name ProcessName -Value (Get-Process -Id $_.OwningProcess).Name -PassThru }
The diagram below shows that two PostgreSQL processes are running along with their process IDs.
As we know from the above diagram, process ID 6628 is using port 5433, and if we insert process ID 6628 into the following command, we can see PostgreSQL 17 is using 5433 port:
Get-Process -Id <PID> | Format-List *
In the same way, we can see that PostgreSQL 14 is running on port 5432.
Identifying the PostgreSQL Port on MacOS
Just like in Linux, to identify your port in macOS, let's first check how many PostgreSQL instances are running on our system by using the following command:
ps -ef | grep -i postgres
502 319 1 0 19Nov24 ?? 0:14.51 /Library/PostgreSQL/15/bin/postmaster -D /Library/PostgreSQL/15/data
502 321 1 0 19Nov24 ?? 0:39.32 /Library/PostgreSQL/16/bin/postgres -D /Library/PostgreSQL/16/data
I have two instances running: PostgreSQL 15 and PostgreSQL 16.
Identifying the port with the installation summary log file
🔖
Note: This method is useful if PostgreSQL was installed with EnterpriseDB’s macOS installers.
Similar to Windows, on macOS, you will find a file called installation_summary.log
, which shows the port number. By default, PostgreSQL is installed in the following directory on macOS:
ls -lhrt /Library/PostgreSQL/
total 0
drwxr-xr-x@ 19 postgres daemon 608B Aug 4 21:48 16
drwxr-xr-x@ 18 postgres daemon 576B Nov 5 10:22 15
To find the port number, run the following command:
cat /Library/PostgreSQL/15/installation_summary.log | grep "Database\ Port"
Database Port: 5433
cat /Library/PostgreSQL/16/installation_summary.log | grep "Database\ Port"
Database Port: 5432
So, PostgreSQL 15 is running on port 5433, and PostgreSQL 16 is running on port 5432.
Identify the port using the process ID
Another way to find the port is by using the process ID (PID).
From the ps
command, we know that PostgreSQL 15 has the process ID 319. Now, run the following command:
netstat -anv | grep 319
tcp4 0 0 *.5433 *.* LISTEN 131072 131072 319 0 00000 00000006 00000000000002c8 00000000 00000900 1 0 000001
tcp6 0 0 *.5433 *.* LISTEN 131072 131072 319 0 00000 00000006 00000000000002c7 00000000 00000800 1 0 000001
This command shows that PostgreSQL 15 is running on port 5433.
For PostgreSQL 16, the process ID is 321. Run the following command:
netstat -anv | grep 321
tcp4 0 0 \*. **5432** \*.\* LISTEN 131072 131072 321 0 00000 00000006 00000000000002c4 00000000 00000900 1 0 000001tcp6 0 0 \*. **5432** \*.\* LISTEN 131072 131072 321 0 00000 00000006 00000000000002c3 00000000 00000800 1 0 000001
This command shows that PostgreSQL 16 is running on port 5432.
Identifying the PostgreSQL Port on Timescale Cloud
When we launch an instance on Timescale Cloud, the configuration provides a connection string that looks like this:
postgres://<USER_NAME>:<PASSWORD>@<HOST>:38081/<DATABASE>?sslmode=require
Here, 38081 is the port being used.
To check the port using the dashboard, simply select your service from the dashboard.
Scroll down to the Connect to your service section. Under this section, you'll find the port number where your instance is running.
Conclusion
Finding the port number for your PostgreSQL instance may seem tricky at first, but with the right tools and commands, it can be a straightforward process. Whether you're a Linux, Windows, or macOS user, I shared simple ways to identify the ports your PostgreSQL servers are running on. By following these steps, you can easily manage your databases and troubleshoot any connection issues. Remember, knowing your system’s configuration is key to keeping everything running smoothly.
If you’re bumping into connection issues with your PostgreSQL database, you may find these resources helpful:
- 5 Common Connection Errors in PostgreSQL and How to Solve Them
- Connecting to PostgreSQL With psql and .pg_service.conf
For more info on PostgreSQL errors, guides to scale your PostgreSQL performance, or best practices, check our Learn PostgreSQL section.
Top comments (0)