Sub topic 6
Controlling which addresses the server listens on
A database server only accepts connections on the addresses and ports it has bound sockets to. This chapter covers the settings that control those sockets in PostgreSQL and MySQL, how to check what the server is actually doing, and the errors that come from getting it wrong. It assumes you can edit files with sudo, know what an IP address, a port and a network interface are, and can use systemctl. It also assumes you know where the configuration files are (see the previous chapter).
Why listen settings come first
Listening is the outermost layer of access control. A connection that never reaches a socket never gets to authentication or privilege checks. Narrowing the listen address does not replace those later layers, but it removes whole classes of exposure. A server bound only to loopback cannot be reached from another host, whatever the firewall or pg_hba.conf says.
A listening socket is defined by an address, a port and a protocol. The address decides which network interface accepts the connection:
| Address | Meaning |
|---|---|
| `127.0.0.1` or `::1` | Loopback only. Only processes on the same host can connect. |
| `10.0.0.5` | One specific interface address. Only traffic arriving at that address is accepted. |
| `0.0.0.0` | Every IPv4 address on the host. |
| `::` | Every IPv6 address, and on most Linux systems IPv4 as well. |
| `*` | Every address. This is a shorthand that PostgreSQL and MySQL both accept. |
Both engines also offer a Unix domain socket, which is a file on disk rather than a network address. Local clients such as psql and mysql use it by default. It needs no listen address and is governed by file permissions.
PostgreSQL: listen_addresses and port
PostgreSQL reads listen_addresses from postgresql.conf. The default is localhost, so a fresh install is not reachable from the network. The value is a comma-separated list of host names or IP addresses.
# postgresql.conf
listen_addresses = 'localhost,10.0.0.5'
port = 5432This binds loopback and one private address. On Debian and Ubuntu the file is /etc/postgresql/<version>/main/postgresql.conf. On Red Hat family systems it is inside the data directory.
Related settings:
portsets the TCP port for every address in the list. There is one port per server instance.unix_socket_directoriessets where the socket file lives. On Debian it is/var/run/postgresql, and the file is named.s.PGSQL.5432.max_connectionslimits total sessions. It matters more once the server is reachable by many hosts.
Changing listen_addresses or port requires a full restart. A reload is not enough:
sudo systemctl restart postgresqlListening on an address does not let that address's clients in. pg_hba.conf still decides who may connect, and that is the subject of the authentication chapter.
MySQL: bind-address and related options
In MySQL, the bind-address option in the [mysqld] section controls the listener.
# MySQL configuration file, [mysqld] section
[mysqld]
bind-address = 10.0.0.5
port = 3306
innodb_buffer_pool_size = 2GThe upstream MySQL default is *, which accepts connections on all IPv4 and IPv6 addresses. Debian and Ubuntu packages ship a file that sets bind-address = 127.0.0.1, usually /etc/mysql/mysql.conf.d/mysqld.cnf. Check the effective value instead of assuming it. Since MySQL 8.0.13, bind-address accepts a comma-separated list, for example 127.0.0.1,10.0.0.5. Older versions accept a single value, so to serve both loopback and a private address you would use 0.0.0.0 and rely on the firewall.
These MySQL options also affect exposure:
mysqlx-bind-addresscontrols the X Protocol listener on port 33060. It is a separate socket, and people often forget it. Restrict it too, or disable it withmysqlx=OFF.skip-networkingturns off TCP entirely. Only the Unix socket (/var/run/mysqld/mysqld.sock) works. This is the right choice when every client runs on the same host.admin-address(8.0.14 and later) opens a separate listener for administrative connections.
Like PostgreSQL, MySQL reads bind-address only at startup, so restart the service after changing it.
The path of a connection
The listen address is one of several checks that a connection passes. Understanding the order helps when you diagnose failures.
The symptoms differ by layer. A firewall drop usually looks like a hang until timeout. A missing listener produces an immediate "connection refused". This difference is the quickest way to tell which layer to inspect first.
Verifying what is actually listening
Never trust the configuration file alone. Ask the kernel what is bound:
ss -ltnp | grep -E ':(5432|3306|33060)'Typical output on a correctly configured PostgreSQL host:
LISTEN 0 244 127.0.0.1:5432 0.0.0.0:* users:(("postgres",pid=912,fd=6))
LISTEN 0 244 10.0.0.5:5432 0.0.0.0:* users:(("postgres",pid=912,fd=7))Read the "Local Address" column. 127.0.0.1:5432 is loopback only. 0.0.0.0:5432 or *:5432 is every IPv4 address. [::]:5432 is every IPv6 address. Use sudo so that -p can show the process name.
You can also ask the running server for its effective values, which catches the case where you edited the wrong file:
-- PostgreSQL
SHOW listen_addresses;
SELECT name, setting, sourcefile FROM pg_settings WHERE name IN ('listen_addresses','port');
-- MySQL
SHOW VARIABLES LIKE 'bind_address';The sourcefile column in pg_settings shows which file set the value, which is useful when several files are included. Finally, test from a client host, because loopback tests prove nothing about remote reachability:
pg_isready -h 10.0.0.5 -p 5432
mysqladmin -h 10.0.0.5 -P 3306 -u appuser -p pingChoosing an address
A practical order of preference:
- Unix socket only, when every client is local (
skip-networkingin MySQL,listen_addresses = ''in PostgreSQL). - Loopback plus one private interface address, as in the examples above.
- A wildcard address, only when the host's interfaces change dynamically and a firewall plus host-based authentication enforce the real policy.
Avoid binding to a public address unless the server must be reachable from the internet. If you must, require TLS and restrict the source networks in the firewall.
Common errors
| Symptom | Likely cause |
|---|---|
| `Connection refused` from a remote host, works locally | Server still bound to loopback, or the restart was not done |
| Connection hangs, then times out | Firewall or cloud security group drops the traffic |
| `could not bind IPv4 address "10.0.0.5": Cannot assign requested address` | The address is not assigned to any interface on this host, or the interface is not up yet at boot |
| `Address already in use` | Another process, or a second instance, holds the port |
| Setting changed but nothing differs | Edited a file that is not read, or only reloaded instead of restarted |
| Remote clients reach port 33060 but not 3306 | `mysqlx-bind-address` set differently from `bind-address` |
The "cannot assign requested address" case appears when a server listens on a virtual or cloud address that comes up after the database service starts. Fix it by ordering the service after network-online.target with a systemd drop-in, or by listening on a wildcard address and restricting access elsewhere.
Exercise
A PostgreSQL server has listen_addresses = 'localhost' in postgresql.conf. You change it to 'localhost,10.0.0.5' and run sudo systemctl reload postgresql. A client on another host still gets "connection refused". What went wrong, and how do you confirm the fix?
Show answer
PostgreSQL reads listen_addresses only at startup, so a reload does not rebind sockets. Run sudo systemctl restart postgresql, then check ss -ltnp | grep 5432 for a 10.0.0.5:5432 entry, and test from the client with pg_isready -h 10.0.0.5. If a connection is then refused with an authentication message instead, the listener works and pg_hba.conf is the next place to look.