Sub topic 18
Troubleshooting common failures
This chapter assumes you have a PostgreSQL or MySQL server installed under systemd, as in the earlier chapters. It also assumes you can read pg_hba.conf and MySQL account host patterns, and that you know what a listening socket is. The goal is a repeatable triage order, so you can find the failing layer from the error text instead of guessing.
Work from the bottom layer up
A client connection passes through several layers. A failure at a lower layer hides everything above it, so check them in order.
Step 1: Is the service running?
Start with systemd, then read the logs for the reason it stopped or never started.
sudo systemctl status postgresql
sudo journalctl -u postgresql --since '15 minutes ago'
ss -ltnp | grep -E ':(5432|3306)'On Debian and Ubuntu, postgresql is a wrapper unit. The real server runs as an instance unit such as postgresql@16-main, so its output is in journalctl -u postgresql@16-main and in /var/log/postgresql/. pg_lsclusters lists the instances and shows whether each is online or down. MySQL logs to the journal under the mysql unit, and on Debian also to /var/log/mysql/error.log. On other distributions the unit may be called mysqld or mariadb.
If the unit is failed, the first error line in the log is usually the cause. Later lines are often consequences.
Startup failures and their causes
| Log message (abbreviated) | Cause | Fix |
|---|---|---|
| `data directory ... has invalid permissions` | PostgreSQL requires mode `0700` (or `0750` with group access enabled) | `sudo chmod 700 <datadir>` and check the owner is `postgres` |
| `could not bind IPv4 address ... Address already in use` | Another process, or a second instance, holds the port | Find it with `ss -ltnp`, stop it or change `port` |
| `invalid value for parameter` or `syntax error in file` | A typo in `postgresql.conf` | Fix the line the log names |
| `No space left on device` | The data or WAL volume is full | Free space, then start the service |
| `Can't create/write to file` or `Permission denied` | Wrong owner after copying files as root, or an AppArmor/SELinux denial | Restore ownership; check `journalctl` or `dmesg` for denials |
| `[ERROR] Unknown variable` or `unknown option` (MySQL) | A setting that is misspelled or not supported by this version | Remove or correct it |
You can check a MySQL configuration without starting the server:
sudo mysqld --validate-configFor PostgreSQL, SELECT * FROM pg_file_settings WHERE error IS NOT NULL; lists configuration errors, but it needs a running server. After a failed reload it is the quickest way to find the bad line.
Disk full is the most common surprise
A full disk stops writes first and then crashes the server. In PostgreSQL, a full WAL volume (the write-ahead log, which records changes before they reach the data files) can prevent startup. Check df -h and df -i, because inodes (file-table entries) can run out before space does. Never delete files from pg_wal by hand. Free space elsewhere, for example old logs or dumps, so the server can finish crash recovery.
Step 2: Is it listening where clients expect?
ss -ltnp shows the address the server actually bound to. The output tells you what to change:
127.0.0.1:5432means the server is bound to loopback only. Remote clients get "connection refused". Setlisten_addresses(PostgreSQL) orbind-address(MySQL).10.0.0.5:5432means remote clients can connect only if they use that address, notlocalhost.- No line at all means the server is not running, or is using only a Unix socket.
Changing listen_addresses or bind-address needs a full restart. A reload does not apply it.
Step 3: Can the packet reach the server?
The failure mode tells you whether a firewall is involved.
- Connection refused (immediate): a host answered that nothing is listening, or the firewall sent a reject. Go back to step 2.
- Timeout (long hang): the packets were silently dropped. This is typical of a firewall
DROP, a missing route, or a cloud security group.
Test from the client host, with the client tools and with a bare TCP check:
pg_isready -h 10.0.0.5 -p 5432
mysqladmin -h 10.0.0.5 -u appuser -p ping
nc -zv 10.0.0.5 5432On the server, run sudo ufw status numbered and confirm a rule allows the client's subnet on the right port. Rules are matched in order, so a broad deny placed earlier can override a later allow.
Step 4: Is authentication accepting this client?
Once TCP connects, the server's own error message is specific. Read it literally.
| Client error | Meaning |
|---|---|
| `FATAL: no pg_hba.conf entry for host "10.0.0.7", user "appuser", database "appdb", SSL encryption` | No rule matches this address, user, database and connection type. A `hostssl` rule will not match a non-TLS connection, and the reverse. |
| `FATAL: password authentication failed for user "appuser"` | A rule matched, but the password is wrong, or the stored hash uses a different method than the rule requires (for example, a `md5` hash with a `scram-sha-256` rule). |
| `ERROR 1045 (28000): Access denied for user 'appuser'@'10.0.0.7'` | MySQL matched an account but the password is wrong, or it matched a different account than you intended. |
| `ERROR 1130: Host '10.0.0.7' is not allowed to connect` | No account exists for that host pattern. |
In MySQL, an account is the pair of user name and host pattern. 'appuser'@'10.0.0.%' does not cover a client at 192.168.1.9. List the accounts to see what exists:
SELECT user, host, plugin FROM mysql.user WHERE user = 'appuser';In PostgreSQL, rules are read from top to bottom and the first match wins. A reject or peer line above your rule can capture the connection. After editing pg_hba.conf, reload instead of restarting:
SELECT pg_reload_conf();
SELECT line_number, type, database, user_name, address, auth_method, error
FROM pg_hba_file_rules;The error column of pg_hba_file_rules flags lines the server could not parse, which would otherwise be skipped.
Step 5: Limits, memory and load
If connections work for some clients but not others, or fail only under load, look at capacity.
FATAL: sorry, too many clients already(PostgreSQL) andERROR 1040: Too many connections(MySQL) meanmax_connectionsis exhausted. Raising it costs memory per connection. Prefer fixing leaking application pools or adding a pooler, as covered in the connection-limits chapter.- A server that dies and restarts with no clear message may have been stopped by the kernel's out-of-memory killer. Check
sudo dmesg -T | grep -i 'out of memory'orjournalctl -k. Oversizedinnodb_buffer_pool_sizeorshared_buffers, orwork_memmultiplied by many connections, are common causes. - TLS failures such as
could not load server certificate fileorprivate key file has group or world accessmean the key file permissions are too open. The key must be owned by the database user and be mode0600, or0640if root owns it and the group is the database group.
A short session
This sequence diagnoses a "connection refused" report from an application host.
# On the database server
systemctl is-active postgresql@16-main
ss -ltn | grep 5432
sudo grep -E '^\s*listen_addresses' /etc/postgresql/16/main/postgresql.conf
# After setting listen_addresses = 'localhost,10.0.0.5'
sudo systemctl restart postgresql@16-main
ss -ltn | grep 5432
# On the application host
pg_isready -h 10.0.0.5The first ss shows only 127.0.0.1:5432, so the fault is the listen setting. After the restart it shows 10.0.0.5:5432, and pg_isready reports accepting connections.
Exercise
A client on 10.0.0.7 gets "connection timed out" when it connects to PostgreSQL at 10.0.0.5:5432. ss -ltn on the server shows 10.0.0.5:5432 listening. pg_hba.conf has a matching hostssl rule for 10.0.0.0/24. Which layer should you investigate next, and why is pg_hba.conf not the cause?
Show answer
Investigate the network path and the firewall (for example sudo ufw status numbered and any cloud security group). A timeout means packets are being dropped before they reach the server. If the request had reached PostgreSQL, the server would have answered with a FATAL message from pg_hba.conf instead of leaving the client waiting.
Exercise
After you edit pg_hba.conf to add a rule, clients still see "no pg_hba.conf entry". Name two likely causes and a command that checks each.
Show answer
First, the change was never loaded: run SELECT pg_reload_conf(); or sudo systemctl reload postgresql. Second, an earlier line matches first, or the rule's connection type does not match (for example hostssl against a non-TLS client): run SELECT line_number, type, database, user_name, address, auth_method, error FROM pg_hba_file_rules; to see the order and any parse errors.