postgresql port confusion 5433 or 5432?

Learn postgresql port confusion 5433 or 5432? with practical examples, diagrams, and best practices. Covers postgresql, port, psql development techniques with visual explanations.

PostgreSQL Port Confusion: Demystifying 5432 vs. 5433

Hero image for postgresql port confusion 5433 or 5432?

Understand the default PostgreSQL port 5432, why you might encounter 5433, and how to manage port configurations for your database instances.

When working with PostgreSQL, one of the first things you'll encounter is the concept of a port number. The default port for PostgreSQL is 5432, a well-known standard. However, it's not uncommon for users to stumble upon 5433 or other port numbers, leading to confusion. This article will clarify why these different ports appear, how to identify which port your PostgreSQL instance is using, and how to configure it correctly.

The Default PostgreSQL Port: 5432

PostgreSQL officially registers and uses port 5432 as its default listening port for client connections. This is a standard convention that most applications and tools expect. When you install PostgreSQL without specifying a custom port, it will typically configure itself to listen on 5432.

psql -h localhost -p 5432 -U your_user -d your_database

Connecting to PostgreSQL using the default port 5432

Why You Might See Port 5433 (or others)

While 5432 is the default, there are several reasons why you might find PostgreSQL running on 5433 or another non-standard port:

  1. Multiple PostgreSQL Instances: If you have more than one PostgreSQL server or cluster running on the same machine, each instance must listen on a unique port. The second instance might default to 5433, the third to 5434, and so on.
  2. Custom Configuration: During installation or later, an administrator might explicitly configure PostgreSQL to use a different port for security, to avoid conflicts, or for specific application requirements.
  3. Development Environments: Tools like Docker, Vagrant, or local development stacks might map internal container ports to different external host ports, or they might run multiple database services, leading to non-standard port assignments.
  4. Port Conflicts: Another service on the system might already be using 5432, forcing PostgreSQL to choose an alternative during installation or startup.
flowchart TD
    A[PostgreSQL Installation] --> B{Is 5432 available?}
    B -->|Yes| C[Listen on 5432]
    B -->|No| D{Multiple Instances?}
    D -->|Yes| E[Listen on 5433 (or next available)]
    D -->|No| F[Custom Port Configured?]
    F -->|Yes| G[Listen on Custom Port]
    F -->|No| H[Error or Default to next available]

Decision flow for PostgreSQL port assignment

Identifying Your PostgreSQL Port

If you're unsure which port your PostgreSQL instance is using, here are a few ways to find out:

  1. Check postgresql.conf: The primary configuration file for PostgreSQL, postgresql.conf, contains the port setting. Its location varies by operating system and installation method (e.g., /etc/postgresql/14/main/postgresql.conf on Debian/Ubuntu, or within your data directory).
  2. psql Command: If you can connect to the database, you can query the current_setting function.
  3. Operating System Tools: Use network utilities like netstat or ss to see which processes are listening on which ports.
SHOW port;

SQL query to show the current port

# On Linux/macOS
sudo netstat -tulnp | grep postgres

# Or using ss (newer and often preferred)
sudo ss -tulnp | grep postgres

Using netstat or ss to find PostgreSQL listening ports

Changing the PostgreSQL Port

To change the port PostgreSQL listens on, you need to modify the postgresql.conf file. This typically involves editing the port parameter and then restarting the PostgreSQL service.

Steps to change the port:

1. Locate postgresql.conf

Find your postgresql.conf file. You can often find its location by running SHOW config_file; in psql if you can connect, or by searching your system.

2. Edit the port parameter

Open postgresql.conf with a text editor and locate the line port = 5432. Change 5432 to your desired port number (e.g., 5433). Make sure the line is uncommented (does not start with #).

3. Restart PostgreSQL service

After saving the changes, restart the PostgreSQL service for the new port to take effect. The command varies by OS, but common examples include sudo systemctl restart postgresql or sudo service postgresql restart.

4. Update client connections

Remember to update any applications, connection strings, or psql commands to use the new port number.