Default port 5432
Learn what a network port is, why PostgreSQL uses port 5432 by default, how to verify the connection works, and how to change the port when needed.
Every program that talks over a network needs an address and a door to knock on. For PostgreSQL, that door is port 5432. This number shows up constantly in connection strings, configuration files, Docker commands, and error messages. Understanding what it means and how it behaves will save you from a large share of PostgreSQL connection problems.
What a port actually is
A computer can run many network programs at the same time: a web server, a database, a chat application. When a message arrives from the network, the operating system needs to know which program should receive it. A port is a number that identifies one specific program listening for network traffic.
Think of an IP address as the street address of an apartment building, and the port as the apartment number. The building address gets mail to the right building. The apartment number gets that mail to the right person inside. Without both, delivery fails.
PostgreSQL chooses to live in apartment 5432 by default. Every PostgreSQL installation on every operating system uses this number unless someone explicitly changes it. That consistency is what allows tools, libraries, and documentation to assume a standard location for the database.
Port numbers are just labels:
Port 5432 is not a physical thing. It is a numeric label that PostgreSQL registers with the operating system when it starts up. Another program could use 5432 if PostgreSQL is not running, and PostgreSQL could use a different port if configured to do so.
Why PostgreSQL picked 5432
Many well-known programs have reserved default ports. Web servers conventionally use port 80 for HTTP and port 443 for HTTPS. PostgreSQL's default of 5432 was assigned by the Internet Assigned Numbers Authority (IANA), the organization that maintains the official registry of port assignments.
The choice itself is not magical. What matters is that the entire PostgreSQL ecosystem agrees on it. When you install PostgreSQL and do nothing else, it listens on 5432. When you run a client tool without specifying a port, it assumes 5432. When a tutorial shows a connection string, it almost always includes 5432. This shared assumption removes an entire category of configuration work.
A connection string is a compact piece of text that tells a program everything it needs to find and authenticate to a database: the host address, the port, the database name, the username, and the password. Here is the typical shape of a PostgreSQL connection string:
postgresql://username:password@localhost:5432/database_name
The port appears after the colon following the host. If you leave out :5432, most PostgreSQL tools still connect correctly because they silently default to that port. The explicit port is often included anyway for clarity.
Checking whether PostgreSQL is listening on 5432
After installing PostgreSQL, the first useful skill is verifying that the server actually accepted the default port and is ready for connections. There are three common ways to check this, and each tells you something slightly different.
Using pg_isready
PostgreSQL ships with a small command-line tool called pg_isready whose only job is to check whether a server is accepting connections. It does not require a username or password. It just knocks on the port and reports what happens.
Open a terminal and run:
pg_isready
If PostgreSQL is running on the default port on your local machine, the output looks like this:
/tmp:5432 - accepting connections
That output confirms two things at once: the server is running, and it answered on port 5432. If PostgreSQL is not running, the output says:
/tmp:5432 - no response
pg_isready can also check a remote host or a non-default port explicitly:
pg_isready -h localhost -p 5432
The -h flag sets the host and the -p flag sets the port. You are unlikely to need these flags for a default local installation, but they become essential when troubleshooting a server that has been moved to a different port.
This is the fastest health check:
If you run pg_isready and see accepting connections, everything about the network layer is working. Authentication might still fail later, but the port question is settled.
Using psql with the port flag
The psql tool is PostgreSQL's interactive terminal client. It connects to a database and lets you run SQL. Unlike pg_isready, it performs a full connection, which includes authentication. This makes it a more realistic test.
Run this command:
psql -h localhost -p 5432 -U postgres -d postgres
You will be prompted for the password of the postgres user if your installation set one, which the graphical installer on Windows does. On installations without a password, like a local Homebrew setup, the connection proceeds without one. If the connection succeeds, the prompt changes to something like:
postgres=#
That prompt means you are inside the PostgreSQL shell and connected through port 5432. You can exit by typing \q and pressing Enter.
The same command works with the connection-string style:
psql "postgresql://postgres@localhost:5432/postgres"
Both forms do the same thing. The explicit flags are easier to read when you are learning. The connection-string form is closer to what you will see in application configuration files later.
Checking the port at the operating system level
Sometimes you want to see the port from the operating system's perspective, independent of PostgreSQL's own tools. On Linux and macOS, the netstat or lsof commands show which process owns which port. On Windows, netstat is also available in PowerShell or Command Prompt.
On Linux or macOS, run:
sudo lsof -i :5432
The output includes the process name (postgres or postmaster), the process ID, and the network address. Seeing LISTEN in the output confirms that PostgreSQL has registered itself on 5432.
On Windows, open PowerShell and run:
netstat -ano | findstr :5432
The output shows the local address ending in :5432 and the process ID in the last column. That process ID belongs to the PostgreSQL server.
Why learn the OS-level check?:
pg_isready only works if PostgreSQL's client tools are installed. The OS-level check works regardless of what client software you have. It also detects the situation where some other program has grabbed port 5432, which PostgreSQL's own tools cannot easily reveal.
Where the port is configured
PostgreSQL reads its port from a configuration file named postgresql.conf. Every PostgreSQL installation has one, though its exact location varies by operating system.
The relevant line looks like this:
port = 5432
If you installed PostgreSQL on Linux through a package manager, the file is typically inside the data directory, often at a path like /etc/postgresql/18/main/postgresql.conf or /var/lib/pgsql/data/postgresql.conf. The exact path depends on the Linux distribution and the PostgreSQL version.
On macOS with a Homebrew installation, the file usually lives at:
/opt/homebrew/var/postgresql@18/postgresql.conf
On Windows with the standard installer, the file is inside the data directory, commonly at:
C:\Program Files\PostgreSQL\18\data\postgresql.conf
You do not need to edit this file during normal use. The default port = 5432 is almost always correct. But knowing where the file lives helps when troubleshooting a changed port or when reading someone else's setup instructions.
The line may also be commented out, which means it begins with a #. PostgreSQL treats a commented-out setting as "use the default," and the default is 5432. Uncommenting it and writing port = 5432 explicitly changes nothing functionally, but it can make the configuration clearer.
What happens when two programs want port 5432
A port can only be owned by one process at a time. If PostgreSQL is already running on 5432 and you try to start a second PostgreSQL instance that also wants 5432, the second instance will fail to start and will write an error to its log. The error usually contains a phrase like:
could not bind to address ... port 5432 is already in use
This is a deliberate safety mechanism. The operating system refuses to deliver the same port to two different programs because doing so would create ambiguity about which program should receive a given network message.
A common beginner mistake is trying to run PostgreSQL both as a native service and inside a Docker container at the same time, with both configured for 5432. The second one to start will fail with exactly this error.
Port conflicts are a real startup blocker:
If PostgreSQL refuses to start and the log mentions port 5432 being in use, do not reinstall PostgreSQL. Find the other program that owns the port first. Reinstalling will not fix the conflict.
The way to inspect the conflict is the OS-level check described earlier. On Linux or macOS, sudo lsof -i :5432 will show the name of the process holding the port. On Windows, netstat -ano | findstr :5432 gives the process ID, which you can match in Task Manager. Once you identify the competing process, you can stop it, or you can move PostgreSQL to a different port.
Changing the port when needed
Most of the time, keeping 5432 is the right choice. Changing it introduces friction: every client must now include the new port explicitly, and every tutorial or example that assumes 5432 will fail silently.
But some situations make a change necessary. You might need to run two PostgreSQL versions side by side, or you might have another application that already occupies 5432, or a corporate policy might require non-standard ports. In those cases, changing the port is straightforward.
Here is the full workflow for changing the port on a local PostgreSQL installation:
Step 1: Locate postgresql.conf
Find the postgresql.conf file for your installation. The paths vary by operating system, as described earlier. If you are unsure where the data directory is, run psql -U postgres -c "SHOW config_file;" to get the full path from PostgreSQL itself.
Step 2: Change the port value
Open postgresql.conf in any text editor. Find the line that says port = 5432, or add it if it does not exist. Change the number to your chosen value, for example 5433. Save the file.
# postgresql.conf - relevant section
port = 5433
Step 3: Restart the PostgreSQL service
The configuration file is only read when PostgreSQL starts. Changing it has no effect on a running server. On Linux with systemd, restart with:
sudo systemctl restart postgresql
On macOS with Homebrew:
brew services restart postgresql@18
On Windows, restart the postgresql-x64-18 service from the Services application, or run PowerShell as Administrator:
Restart-Service postgresql-x64-18
Step 4: Verify the new port
Run pg_isready with the new port explicitly:
pg_isready -p 5433
If it reports accepting connections, the change worked. You can also check that the old port is now silent:
pg_isready -p 5432
That command should report no response.
Every future connection must include the new port:
After changing the port, any tool or application that used to connect without specifying a port will now fail. A connection string like postgresql://postgres@localhost/postgres assumes 5432 and will no longer work. It must become postgresql://postgres@localhost:5433/postgres.
The most important detail in this workflow is the restart step. Editing a configuration file while the server is running does nothing. PostgreSQL does not watch the file for changes. The new value only takes effect after a full stop and start.
Connecting an application to PostgreSQL on 5432
The reason the port matters is that your future Axum application will need to reach PostgreSQL. The connection happens through a library, not through psql, but the network details are identical.
A typical database connection configuration in a Rust application using sqlx looks like this. The two dependencies it needs are sqlx with the postgres and runtime-tokio features, and tokio with the macros and rt-multi-thread features, because #[tokio::main] needs both. Add them to your Cargo.toml:
[dependencies]
sqlx = { version = "0.9", features = ["postgres", "runtime-tokio"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
Then the connection code itself, in src/main.rs, looks like this:
use sqlx::postgres::PgPoolOptions;
#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
let database_url = "postgresql://postgres:password@localhost:5432/mydb";
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(database_url)
.await?;
println!("Connected to PostgreSQL on port 5432");
Ok(())
}
The connection string in database_url is the same shape you used in psql. The @localhost part means "this machine," and the :5432 part is the port. If the server were on a different machine, localhost would be replaced by that machine's IP address or hostname, but the port would remain 5432 unless the server was customized.
The PgPoolOptions object is part of sqlx, a Rust library for talking to SQL databases. A pool is a small set of reusable database connections that stay open, so your application does not pay the cost of opening a fresh connection for every single request. None of this would work without the correct port in the connection string.
The port is part of the address, not the authentication:
If the port is wrong, the connection fails before PostgreSQL ever checks a username or password. Error messages about "connection refused" or "could not connect to server" often mean a wrong port or a stopped server, not a bad password.
Port 5432 in Docker and other tools
Docker changes the picture slightly because it runs PostgreSQL inside an isolated container. The container has its own internal port, and that port can be mapped to a port on your host machine.
A typical Docker command for PostgreSQL looks like this:
docker run -d --name my-postgres \
-p 5432:5432 \
-e POSTGRES_PASSWORD=mysecretpassword \
-e POSTGRES_DB=mydb \
postgres:18
The -p 5432:5432 flag is the port mapping. The first number is the port on your host machine, the one your applications connect to. The second number is the port inside the container, where PostgreSQL itself listens. Docker receives traffic on the host port and forwards it to the container port.
This means you can run a containerized PostgreSQL on host port 5432 just like a native installation. From the perspective of psql and your Rust application, there is no difference. The connection string is still postgresql://postgres:mysecretpassword@localhost:5432/mydb.
You can also move the host port without touching the container's internal port. The command:
docker run -d --name my-postgres \
-p 5433:5432 \
-e POSTGRES_PASSWORD=mysecretpassword \
postgres:18
maps host port 5433 to container port 5432. Inside the container, PostgreSQL is still using its default. Outside the container, clients must connect to 5433. This is a clean way to run multiple PostgreSQL instances side by side without editing any postgresql.conf file.
Docker port mapping does not edit PostgreSQL configuration:
Changing the -p mapping only changes what the host exposes. It does not change the port PostgreSQL listens on inside the container. Confusing the two leads to connection strings that look correct but target the wrong side of the mapping.
What beginners most often get wrong
The most common port-related beginner mistake is assuming a connection failure means PostgreSQL is broken, when in fact another program is already using 5432 or the server simply has not been started. The second most common mistake is changing the port in a configuration file, forgetting to restart, and then wondering why nothing changed.
A more subtle mistake involves localhost versus 127.0.0.1. They usually mean the same machine, but some PostgreSQL installations are configured to listen only on a Unix socket for local connections, not on the TCP port. A Unix socket is a special file-based communication channel that bypasses network ports entirely. If psql without -h localhost works but psql -h localhost -p 5432 fails, the server may not be listening on TCP at all.
On most standard installs, PostgreSQL does listen on TCP port 5432 by default. But if you encounter this specific failure pattern, the relevant configuration line in postgresql.conf is:
listen_addresses = 'localhost'
When listen_addresses is set to 'localhost', PostgreSQL accepts TCP connections on port 5432 from the local machine only. When it is empty, only Unix socket connections work. When it is '*', PostgreSQL accepts connections on all network interfaces, which is necessary if other machines need to reach the database.
Do not open the port to the internet by default:
Setting listen_addresses = '*' makes PostgreSQL reachable from other machines. If you do this on a computer with a public network interface and a weak password, you have exposed your database to the entire internet. For local development, 'localhost' is the correct and safe choice.
A small mental model for the whole thing
When your future Rust application connects to PostgreSQL, the sequence is always the same, and the port sits in the middle of it. The client library builds a connection request using the host and port from the connection string. The operating system routes that request to whatever process is listening on that port. If PostgreSQL is listening on 5432, it receives the request and begins the authentication handshake. If nothing is listening, the request dies with a "connection refused" error. If a different program is listening, that program receives a request it does not understand and the connection fails or hangs.
This model explains every symptom you will ever see around ports. A working connection means all parts of the chain are correct. A refused connection means no process owns the port. A timeout means something is listening but not responding, or a firewall is blocking the path. A protocol error means the wrong program owns the port.
Summary
Port 5432 is PostgreSQL's default network door, and treating it as a fixed part of the PostgreSQL experience is correct for almost every local development setup. The value is not arbitrary: it is the registered convention that every PostgreSQL tool, library, and tutorial relies on. When a connection fails, the port is one of the first things to check, because it is one of the few pieces of the connection chain that can silently drift out of alignment while everything else looks correct.
The practical skill to carry forward is not memorizing the number, but knowing how to verify it with pg_isready, how to inspect it with lsof or netstat, where it lives in postgresql.conf, and what a changed port requires from every future connection string. That complete picture turns a confusing numeric error into a solvable configuration question.