Reference · Databases
PostgreSQL port 5432, MySQL ports 3306 and 33060: check where the database listens
Check which addresses PostgreSQL, MySQL and MariaDB listen on: listen_addresses and trust rules in pg_hba.conf, bind-address and mysqlx-bind-address in mysqld.cnf or MariaDB's 50-server.cnf, then confirm with ss -tlnp.
| Target artifact | PostgreSQL's listen_addresses in postgresql.conf and the rules in pg_hba.conf, for TCP port 5432; MySQL's bind-address for port 3306 and mysqlx-bind-address for the X Protocol port 33060; MariaDB's bind-address for port 3306. |
|---|---|
| Default behaviour | PostgreSQL listens on localhost. MySQL's own default for both addresses is *, every interface, but Ubuntu's mysqld.cnf sets both to 127.0.0.1, and Ubuntu's MariaDB file 50-server.cnf sets bind-address = 127.0.0.1. A file read later can override them. |
| Safe state | ss shows ports 5432, 3306 and 33060 only on 127.0.0.1 or ::1, or on a private address that the firewall opens to the app servers alone; no trust method on host lines in pg_hba.conf; no database account that can sign in from any host unless it needs to. |
| Check on Linux | sudo ss -tlnp '( sport = :5432 or sport = :3306 or sport = :33060 )' |
| Check in PostgreSQL | sudo -u postgres psql -c 'SHOW listen_addresses;' |
| Check in MySQL | sudo mysql -e 'SELECT @@bind_address, @@mysqlx_bind_address;' |
| Fix for PostgreSQL | listen_addresses = 'localhost' in /etc/postgresql/VERSION/main/postgresql.conf, no trust on host lines in pg_hba.conf, then sudo systemctl restart postgresql |
| Fix for MySQL and MariaDB | bind-address = 127.0.0.1, plus mysqlx-bind-address = 127.0.0.1 for MySQL, in the last file that sets them, then restart mysql or mariadb |
Read the result
Address in ss | Who can connect |
|---|---|
127.0.0.1 or [::1] before the port | This computer only |
0.0.0.0, [::] or * before the port | Every network the server is on, unless a firewall stops them |
A private address, such as 10.0.0.5 | Machines that can reach that network |
From another device, nc -vz -w 5 SERVER_IP 5432 (or 3306, 33060) shows whether the port is reachable. The database's own client says more:
| Reply | Meaning |
|---|---|
psql: Connection refused; mysql: ERROR 2003 ending in (111) | Nothing listens on that address |
psql: no pg_hba.conf entry for host | PostgreSQL listens on the network, but no rule admits your address |
psql: a password prompt, or fe_sendauth: no password supplied | Reachable, and a rule asks for a password |
| psql connects with no password asked | A trust rule: anyone who reaches the port can sign in as any user. Fix it now |
mysql: ERROR 1045 (28000): Access denied | The server answered, so the port is reachable |
mysql: ERROR 1130 (HY000), host is not allowed to connect | The server answered, so the port is reachable; no account accepts sign-ins from your address |
PostgreSQL
On Ubuntu, the configuration is in /etc/postgresql/VERSION/main/: VERSION is 14 on Ubuntu 22.04, 16 on 24.04 and 18 on 26.04, and pg_lsclusters lists each cluster with its port. Ubuntu's documentation says only local connections are allowed by default.
PostgreSQL's documentation says trust lets anyone who can connect sign in as any PostgreSQL user, with no password. initdb, which creates a cluster, uses trust when no method is given; Ubuntu's packages create the main cluster with peer for local socket connections and scram-sha-256 for TCP from 127.0.0.1 and ::1.
Check listen_addresses and pg_hba.conf
pg_lsclusters
sudo -u postgres psql -c 'SHOW listen_addresses;'
sudo -u postgres psql -c 'SELECT line_number, type, database, user_name, address, netmask, auth_method, error FROM pg_hba_file_rules;'localhostmeans loopback only;*,0.0.0.0or::means every interface.- The first rule that matches the connection type, address, database and user decides, with no fall-through. A
hostline withtrustfor0.0.0.0/0,::0/0oralladmits every address: in a test on 9 November 2026 with PostgreSQL 16, such a line let a connection from a network address sign in aspostgreswith no password. pg_hba_file_rulesreads the file as it is now: a value inerrormarks a broken line before you reload.
Fix: loopback only, and no trust on host lines
- In
/etc/postgresql/16/main/postgresql.conf(use your version), set:
listen_addresses = 'localhost'- In
pg_hba.confin the same folder, changetruston everyhostline toscram-sha-256, and delete lines for addresses that need no access. Accounts that connect over TCP then need a password. listen_addresseschanges only at a restart;pg_hba.confneeds only a reload. On Ubuntu,postgresqlrestarts or reloads every cluster:
sudo systemctl restart postgresql
sudo ss -tlnp 'sport = :5432'Other machines must connect
List the server's private address after localhost, as in listen_addresses = 'localhost,10.0.0.5', add a host or hostssl line with scram-sha-256 for each app server's address, and open port 5432 to those addresses alone; Guide 5, step 4 has the UFW rules. trust on such a line would admit anyone who reaches the port.
MySQL
| Setting | MySQL's default | Ubuntu's mysqld.cnf |
|---|---|---|
bind-address, port 3306 | *: every IPv4 and IPv6 interface | 127.0.0.1 |
mysqlx-bind-address, X Protocol port 33060 | * | 127.0.0.1 |
X Plugin, which serves the X Protocol on port 33060, is enabled by default in MySQL 8.4. Ubuntu 22.04 and 24.04 ship MySQL 8.0 and 26.04 ships 8.4, all with /etc/mysql/mysql.conf.d/mysqld.cnf. The two addresses are separate: in a test with MySQL 8.0.46 on Ubuntu 24.04, a later file that set only bind-address = 0.0.0.0 opened port 3306 and left 33060 on 127.0.0.1.
Check MySQL
sudo ss -tlnp '( sport = :3306 or sport = :33060 )'
sudo mysql -e 'SELECT @@bind_address, @@mysqlx_bind_address;'
sudo mysqld --verbose --help 2>/dev/null | grep -E '^(bind-address|mysqlx-bind-address) '
sudo grep -rn 'bind-address' /etc/mysql/SELECT shows what the running server uses; mysqld --verbose --help shows what the next start will use, where a later option file wins. sudo mysql works because MySQL's root uses auth_socket, sign-in through the local socket, on Ubuntu 24.04. Then list accounts that can sign in from elsewhere; a host of % matches any host:
sudo mysql -e "SELECT user, host FROM mysql.user WHERE host NOT IN ('localhost', '127.0.0.1', '::1');"Fix MySQL
Set both lines in the file that sets them last, usually mysqld.cnf:
bind-address = 127.0.0.1
mysqlx-bind-address = 127.0.0.1If nothing uses the X Protocol, mysqlx = OFF under [mysqld] turns X Plugin off; in the same test, port 33060 closed, and @@mysqlx_bind_address then gives Unknown system variable. bind_address cannot change while the server runs: sudo systemctl restart mysql, then check ss again.
MariaDB
Ubuntu's MariaDB packages set bind-address = 127.0.0.1 in /etc/mysql/mariadb.conf.d/50-server.cnf: under [mysqld] for MariaDB 10.6 on Ubuntu 22.04 and 10.11 on 24.04, and under [mariadbd] for 11.8 on 26.04. MariaDB's guide says that commenting the line out makes the server listen on all interfaces, and a comma-separated list of addresses needs MariaDB 10.11 or later. The file has no X Protocol setting.
Check and fix MariaDB
sudo ss -tlnp 'sport = :3306'
sudo mariadb -e 'SELECT @@bind_address;'
sudo grep -rn 'bind-address' /etc/mysql/sudo mariadb works because MariaDB's root account signs in through the Unix socket after a package install. Set bind-address = 127.0.0.1 in 50-server.cnf, remove any later line that overrides it, then run sudo systemctl restart mariadb.
Docker
The official postgres image adds a host all all all rule using POSTGRES_HOST_AUTH_METHOD, scram-sha-256 by default since PostgreSQL 14; its documentation warns that trust lets anyone connect without a password. -p 5432:5432 publishes the port on every host address, past UFW: write -p 127.0.0.1:5432:5432, and the same for MySQL and MariaDB (Docker published ports).
Troubleshooting
The file says 127.0.0.1, but ss still shows 0.0.0.0
The server has not restarted since the change, a file read later sets the address again (sudo grep -rn finds it), or a container publishes the port (docker ps).
An app on another server can no longer connect
Use an SSH tunnel from that server, as in ssh -N -L 5432:127.0.0.1:5432 USER@DB_SERVER, or a private address as above.
Related guides and lookups
Sources
- PostgreSQL: connections and authentication
- PostgreSQL: the pg_hba.conf file
- PostgreSQL 16: initdb
- Ubuntu Server: install and configure PostgreSQL
- MySQL 8.4 manual: server system variables
- MySQL 8.4 manual: X Plugin options and system variables
- MySQL 8.4 manual: specifying account names
- MariaDB: configuring MariaDB for remote client access
- MariaDB: system variables added in 10.3
- MariaDB: differences in MariaDB in Debian and Ubuntu
- Docker official images: postgres
- Docker: port publishing