- Learn
- PostgreSQL
- PostgreSQL default port
PostgreSQL default port
PostgreSQL's default port is 5432. Here's how to check it, change it, and connect.
The default port
PostgreSQL listens on port 5432 by default. This is its assigned port from IANA and is used by every major PostgreSQL distribution unless you change it. If you are connecting to a PostgreSQL server and have not configured a custom port, 5432 is what you want.
Checking which port PostgreSQL is using
Inside psql
Run this from any psql session:
Output:
port
------
5432
(1 row)
On Debian and Ubuntu
The pg_lsclusters command lists all PostgreSQL clusters on the machine, including their version, name, status, and port:
Output:
Ver Cluster Port Status Owner Data directory Log file
15 main 5432 online postgres /var/lib/postgresql/15/main /var/log/postgresql/postgresql-15-main.log
16 main 5433 online postgres /var/lib/postgresql/16/main /var/log/postgresql/postgresql-16-main.log
With ss or netstat
To see which port the PostgreSQL process is bound to at the OS level:
Or if your system uses netstat:
Output:
LISTEN 0 128 0.0.0.0:5432 0.0.0.0:* users:(("postgres",pid=1234,fd=3))
In postgresql.conf
The port is set in postgresql.conf. To find the file location, run SHOW config_file; in psql. Then look for the port line:
port = 5432
If the line is commented out (prefixed with #), PostgreSQL is using the compiled-in default of 5432.
Connecting to a specific port
psql flag
Use -p to specify a port:
Connection string
Include the port in the connection URI:
postgresql://postgres:password@localhost:5433/myapp
PGPORT environment variable
Set PGPORT to avoid repeating -p in every command:
Most PostgreSQL clients and libraries respect PGPORT as a fallback when no port is specified in the connection string.
Changing the port
Open postgresql.conf and update the port line:
port = 5433
Then restart PostgreSQL. A reload is not enough because the port is set at startup:
On systems with multiple PostgreSQL versions, specify the version in the service name:
Verify the change took effect:
Any clients connecting to the old port will fail after the restart. Update their connection strings or PGPORT accordingly.
Running multiple PostgreSQL instances
When you install multiple PostgreSQL versions on the same machine, each instance needs a different port. A common convention on Debian and Ubuntu:
| Version | Port | |---------|------| | PostgreSQL 14 | 5432 | | PostgreSQL 15 | 5432 (or 5433 if 14 is already running) | | PostgreSQL 16 | 5433 |
pg_lsclusters shows the port for each cluster. To connect to a specific version, use -p with the correct port:
When running two instances as part of a replication setup or a migration, keep a note of which port belongs to which cluster. Connecting to the wrong one is a common source of confusion.
Firewall rules
If PostgreSQL needs to accept connections from other machines, you need to open port 5432 in the firewall.
ufw
To restrict access to a specific IP address rather than allowing all traffic:
iptables
Security note: Opening port 5432 to 0.0.0.0 (all interfaces) in production exposes PostgreSQL to the public internet. Use IP allowlisting, a VPN, or an SSH tunnel instead of leaving the port fully open. Also make sure pg_hba.conf requires passwords for all non-local connections.
Common database ports for reference
| Database | Default port | |----------|-------------| | PostgreSQL | 5432 | | MySQL / MariaDB | 3306 | | Redis | 6379 | | MongoDB | 27017 |
Quick reference
| Task | Command |
|------|---------|
| Check port in psql | SHOW port; |
| List clusters with ports (Debian/Ubuntu) | pg_lsclusters |
| Check port at OS level | ss -tlnp \| grep postgres |
| Connect to a specific port | psql -p 5433 -U postgres |
| Set port via environment variable | export PGPORT=5433 |
| Change the port | Edit port in postgresql.conf, then restart |
| Restart PostgreSQL | sudo systemctl restart postgresql |
| Allow port through ufw | sudo ufw allow 5432/tcp |