SaferComputing.com

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.

Published
Updated
Last checked
Applies to
PostgreSQL, MySQL and MariaDB from the Ubuntu 22.04, 24.04 and 26.04 packages, and other Linux systems with their own file paths
Technical specification
Target artifactPostgreSQL'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 behaviourPostgreSQL 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 statess 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 Linuxsudo ss -tlnp '( sport = :5432 or sport = :3306 or sport = :33060 )'
Check in PostgreSQLsudo -u postgres psql -c 'SHOW listen_addresses;'
Check in MySQLsudo mysql -e 'SELECT @@bind_address, @@mysqlx_bind_address;'
Fix for PostgreSQLlisten_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 MariaDBbind-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 ssWho can connect
127.0.0.1 or [::1] before the portThis computer only
0.0.0.0, [::] or * before the portEvery network the server is on, unless a firewall stops them
A private address, such as 10.0.0.5Machines 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:

ReplyMeaning
psql: Connection refused; mysql: ERROR 2003 ending in (111)Nothing listens on that address
psql: no pg_hba.conf entry for hostPostgreSQL listens on the network, but no rule admits your address
psql: a password prompt, or fe_sendauth: no password suppliedReachable, and a rule asks for a password
psql connects with no password askedA trust rule: anyone who reaches the port can sign in as any user. Fix it now
mysql: ERROR 1045 (28000): Access deniedThe server answered, so the port is reachable
mysql: ERROR 1130 (HY000), host is not allowed to connectThe 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
Ubuntu, as a user with sudo
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;'
  • localhost means loopback only; *, 0.0.0.0 or :: means every interface.
  • The first rule that matches the connection type, address, database and user decides, with no fall-through. A host line with trust for 0.0.0.0/0, ::0/0 or all admits every address: in a test on 9 November 2026 with PostgreSQL 16, such a line let a connection from a network address sign in as postgres with no password.
  • pg_hba_file_rules reads the file as it is now: a value in error marks a broken line before you reload.
Fix: loopback only, and no trust on host lines
  1. In /etc/postgresql/16/main/postgresql.conf (use your version), set:
Line in /etc/postgresql/16/main/postgresql.conf
listen_addresses = 'localhost'
  1. In pg_hba.conf in the same folder, change trust on every host line to scram-sha-256, and delete lines for addresses that need no access. Accounts that connect over TCP then need a password.
  2. listen_addresses changes only at a restart; pg_hba.conf needs only a reload. On Ubuntu, postgresql restarts or reloads every cluster:
Ubuntu, as a user with sudo
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

SettingMySQL's defaultUbuntu's mysqld.cnf
bind-address, port 3306*: every IPv4 and IPv6 interface127.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
Ubuntu, as a user with sudo
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:

Ubuntu, as a user with sudo
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:

Lines in /etc/mysql/mysql.conf.d/mysqld.cnf, under [mysqld]
bind-address        = 127.0.0.1
mysqlx-bind-address = 127.0.0.1

If 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
Ubuntu, as a user with sudo
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.

Sources