Port 5432 (PostgreSQL)
Key facts
- Port
- 5432
- Protocols
- TCP
- Range
- Registered port, 1024-49151
- IANA service name
- postgresql
IANA assigns ports in this range to services on request, under RFC 6335.
About port 5432
Port 5432 is the default port of the PostgreSQL database, which applications and admin tools use to connect to the database server, over TCP. In the official PostgreSQL documentation, the port setting defines the port, and its default value is 5432.
IANA registers port 5432 for the postgresql service.
What port 5432 is used for
- Application and database on the same server: the application connects to the database through localhost or a local socket file, with no need for a port open to the outside.
- Separate database server: application servers connect to port 5432 on the database server, usually over a private network.
- Admin tools: the psql tool and graphical admin tools connect to port 5432, directly or through an SSH tunnel.
- Replication: secondary servers connect to the primary server on the same port to receive changes.
Port 5432 in the firewall
By default, PostgreSQL listens only on localhost (the listen_addresses setting), so when the application and the database are on the same server, there's no need to open port 5432. When access from another server is needed, add the internal network address to listen_addresses, allow the source address in the pg_hba.conf file, and open the port in the firewall only to specific addresses:
- UFW:
ufw allow from 203.0.113.10 to any port 5432 proto tcp - firewalld:
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10" port port="5432" protocol="tcp" accept'
For occasional work from your computer, an SSH tunnel lets you connect to the database without opening port 5432 at all.
Risks and security
PostgreSQL has two layers of protection: the listen_addresses setting controls which addresses the server accepts connections on, and the pg_hba.conf file controls which connections are allowed to log in. Alongside the firewall, a few simple habits complete the protection:
- Create a separate user for each application, with privileges only on its own database.
- In the pg_hba.conf file, allow access only from specific addresses, with password authentication using the scram-sha-256 method.
- When connecting from another computer over the internet, encrypt the connection with TLS or route it through an SSH tunnel.
- Back up your databases regularly, and keep the backups off the server.
Commands to check and configure
-
ss -tlnp | grep ':5432 'Shows which address PostgreSQL is listening on: 127.0.0.1 means local access only, and 0.0.0.0 or * means all addresses.
-
nc -zv db.example.com 5432Checks whether port 5432 on the database server is reachable from your computer.
-
pg_isready -h db.example.com -p 5432Checks whether the PostgreSQL server is accepting connections on the given address and port.
-
psql -h db.example.com -p 5432 -U appuser -d appdbConnects to a remote database as a specific user. The password is prompted for separately.
-
sudo -u postgres psql -c "SHOW listen_addresses;"Shows the addresses PostgreSQL is set to listen on.
-
sudo -u postgres psql -c "SELECT pg_reload_conf();"Reloads the settings, for example after a change to the pg_hba.conf file.
-
ssh -L 5433:127.0.0.1:5432 user@server.example.comOpens an SSH tunnel: the admin tool on your computer connects to 127.0.0.1 on port 5433, and the connection reaches the database on the server.
-
ufw allow from 203.0.113.10 to any port 5432 proto tcpAllows access to port 5432 only from the given IP address (UFW).
-
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10" port port="5432" protocol="tcp" accept'Allows access to port 5432 in firewalld only from the given IP address. Then run firewall-cmd --reload.
Port 5432 in the IANA registry
| Port | Protocol | Service | IANA registry description |
|---|---|---|---|
| 5432 | TCP | postgresql | PostgreSQL Database |
| 5432 | UDP | postgresql | PostgreSQL Database |
Data source: IANA Service Name and Transport Protocol Port Number Registry (opens in a new tab)
Port 5432 FAQ
Do I need to open port 5432 for my application to work?
No, not when the application and the database are on the same server. In that case, the application connects through localhost or a local socket file, and the port can stay closed in the firewall. Open port 5432 only when an application on another server needs to connect, and only to that server's IP addresses.
How do I connect securely to PostgreSQL on my server from my computer?
The recommended way is an SSH tunnel. Popular database tools support connecting over SSH, so the connection travels encrypted over port 22 and port 5432 stays closed to the internet.
What does the no pg_hba.conf entry error mean?
The error means the pg_hba.conf file has no line that allows this connection, based on the source address, the user, and the database. Add a matching line for the address of the connecting server, and reload the settings, for example with SELECT pg_reload_conf();.
Can I run several PostgreSQL instances on the same server?
Yes. Each PostgreSQL instance listens on its own port, for example 5432 and 5433, and you specify the port with the -p option when you connect. This way you can run, for example, a test environment alongside the production environment.